题
我试图开发一种TCP客户端上使用MIDP 2.0和CLDC 1.1移动设备运行。我试图一些示例代码,和我有以下问题:
我得到一个奇怪的例外,当我尝试读回数据(从MIDlet的)。
这是我的代码:
//Wait for an incoming message
firstByte = in.read();
ByteArrayOutputStream textRecieved = new ByteArrayOutputStream(); //Will be used to hold the data
if (firstByte >= 0 )
{
int messageSize = this.in.available();
//Read the message
while (messageSize > 0)
{
byte[] buffer = new byte[messageSize];
this.in.read(buffer);
textRecieved.write(buffer);
messageSize = this.in.available(); //Just in case the server sent the request in chunks.
System.out.println("Reading...");
}
}
textRecieved.close();
这是个例外我得到:
java.io.IOException: Unknown error 10053 during socket::read at com.sun.midp.io.j2me.socket.Protocol.read0(), bci=0 at com.sun.midp.io.j2me.socket.Protocol.nonBufferedRead(), bci=12 at com.sun.midp.io.BufferedConnectionAdapter.readBytes(), bci=36 at com.sun.midp.io.BaseInputStream.read(), bci=227 at com.sun.midp.io.BufferedInputStream.fill(), bci=172 at com.sun.midp.io.BufferedInputStream.read(), bci=16 at hello.Client.run22222(Client.java:60) at hello.HelloMIDlet.startApp(HelloMIDlet.java:193) at javax.microedition.midlet.MIDletTunnelImpl.callStartApp(), bci=1 at com.sun.midp.midlet.MIDletPeer.startApp(), bci=7 at com.sun.midp.midlet.MIDletStateHandler.startSuite(), bci=269 at com.sun.midp.main.AbstractMIDletSuiteLoader.startSuite(), bci=52 at com.sun.midp.main.CldcMIDletSuiteLoader.startSuite(), bci=8 at com.sun.midp.main.AbstractMIDletSuiteLoader.runMIDletSuite(), bci=161 at com.sun.midp.main.AppIsolateMIDletSuiteLoader.main(), bci=26
这导致异常的行是这样的:
firstByte = in.read();
我读一个单独的线程。我经历了同样的错误,当我发送的请求,并试图使用相同的服务器来读取。服务器是一个简单的回声服务器,没有什么复杂。
P.S。我知道,这样的代码编写看起来像C#,但它是Java,我觉得更容易阅读,并按照这种方式。
感谢。
解决方案 2
此实例似乎是工作: HTTP://www.java-samples。 COM / J2ME /插座自由连接-J2ME的采样program.htm
难道的问题是,我正在执行客户机中作为由所述的midlet称为类?
其他提示
我看到的第一件事情是,你误用available()
。它最有可能的不的你希望它做什么。
available()
返回的估计。它的JavaDoc接着明确说明这一点:
这是永远不会正确使用这种方法的返回值分配缓冲区用于保持在该流中的所有数据。
这可以很好地只返回0
,在这种情况下,你分配大小为0的缓冲区,并尝试读成。
在一个符合标准的实现应该只是立即返回,而不是读什么,但你可以很容易引发错误的与低于完美的执行。
不隶属于 StackOverflow