java.net.SocketException: 软件导致连接中止: 套接字写入错误
2022-09-04 04:30:58
我正在尝试将图像从 Java 桌面应用程序发送到 J2ME 应用程序。问题是我得到了这个例外:
java.net.SocketException: Software caused connection abort: socket write error
我在网上环顾四周,虽然这个问题并不罕见,但我无法找到具体的解决方案。在传输之前,我正在将图像转换为字节数组。这些分别在桌面应用程序和 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();
}
我通过 LAN 发送消息,当我发送短文本消息而不是图像时,我没有问题。另外,我使用了wireshark,似乎桌面应用程序只发送了部分消息。任何帮助将不胜感激。此外,一切都可以在J2ME模拟器上工作。