npi*_*nti 13 java networking java-me
我正在尝试将图像从Java桌面应用程序发送到J2ME应用程序.问题是我得到了这个例外:
java.net.SocketException: Software caused connection abort: socket write error
Run Code Online (Sandbox Code Playgroud)
我在网上四处看看,尽管这个问题并不罕见,但我无法找到具体的解决方案.我在传输之前将图像转换为字节数组.这些是分别在桌面应用程序和J2ME上找到的方法
public void send(String ID, byte[] serverMessage) throws Exception
{
//Get the IP and Port of the person to which the message is to be sent.
String[] connectionDetails = this.userDetails.get(ID).split(",");
Socket sock = new Socket(InetAddress.getByName(connectionDetails[0]), Integer.parseInt(connectionDetails[1]));
OutputStream os = sock.getOutputStream();
for (int i = 0; i < serverMessage.length; i++)
{
os.write((int) serverMessage[i]);
}
os.flush();
os.close();
sock.close();
}
private void read(final StreamConnection slaveSock)
{
Runnable runnable = new Runnable()
{
public void run()
{
try
{
DataInputStream dataInputStream = slaveSock.openDataInputStream();
int inputChar;
StringBuffer results = new StringBuffer();
while ( (inputChar = dataInputStream.read()) != -1)
{
results.append((char) inputChar);
}
dataInputStream.close();
slaveSock.close();
parseMessage(results.toString());
results = null;
}
catch(Exception e)
{
e.printStackTrace();
Alert alertMsg = new Alert("Error", "An error has occured while reading a message from the server:\n" + e.getMessage(), null, AlertType.ERROR);
alertMsg.setTimeout(Alert.FOREVER);
myDisplay.setCurrent(alertMsg, resultScreen);
}
}
};
new Thread(runnable).start();
}
Run Code Online (Sandbox Code Playgroud)
我通过局域网发送消息,当我发送短信而不是图像时,我没有问题.此外,我使用wireshark,似乎桌面应用程序只发送部分消息.任何帮助将受到高度赞赏.此外,一切都在J2ME模拟器上运行.
请参阅"软件导致连接中止:套接字写入错误"的官方原因的答案
编辑
我认为一般来说还有更多可以说的内容,并且您的代码似乎没有任何异常会导致连接中止.但我会注意到:
write
调用是不必要的.它会自动升级.write(byte[])
而不是更好(更简单,在网络流量方面可能更有效)write(int)
. 归档时间: |
|
查看次数: |
71946 次 |
最近记录: |