我正在尝试开发一个在使用 MIDP 2.0 和 CLDC 1.1 的移动设备上运行的 TCP 客户端。我正在尝试一些示例代码,但遇到以下问题:
当我尝试读回数据(来自 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:socket::read 期间出现未知错误 10053 在 com.sun.midp.io.j2me.socket.Protocol.read0(), bci=0 在 com.sun.midp.io.j2me.socket.Protocol.nonBufferedRead(),bci=12 在 com.sun.midp.io.BufferedConnectionAdapter.readBytes(),bci=36 在 com.sun.midp.io.BaseInputStream.read(),bci=227 在 com.sun.midp.io.BufferedInputStream.fill(),bci=172 在 com.sun.midp.io.BufferedInputStream.read(),bci=16 在 hello.Client.run22222(Client.java:60) 在 hello.HelloMIDlet.startApp(HelloMIDlet.java:193) 在 javax.microedition.midlet.MIDletTunnelImpl.callStartApp(),bci=1 在 com.sun.midp.midlet.MIDletPeer.startApp(),bci=7 在 com.sun.midp.midlet.MIDletStateHandler.startSuite(),bci=269 在 com.sun.midp.main.AbstractMIDletSuiteLoader.startSuite(), bci=52 在 com.sun.midp.main.CldcMIDletSuiteLoader.startSuite(), bci=8 在 com.sun.midp.main.AbstractMIDletSuiteLoader.runMIDletSuite(), bci=161 在 com.sun.midp.main.AppIsolateMIDletSuiteLoader.main(), bci=26
导致异常的行是这样的:
firstByte = in.read();
我正在阅读一个单独的线程。当我发送请求并尝试使用同一服务器读取它们时,我遇到了同样的错误。服务器是一个简单的回显服务器,没什么复杂的。
PS 我知道代码的编写方式看起来像 C#,但它是 Java,我发现这种方式更易于阅读和遵循。
谢谢。