java.net.SocketException:软件导致连接中止:套接字写入错误
我正在尝试将图像从 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 模拟器上运行。
I am trying to send an image from a Java desktop application to a J2ME application. The problem is that I am getting this exception:
java.net.SocketException: Software caused connection abort: socket write error
I have looked around on the net, and although this problem is not that rare, I was unable to find a concrete solution. I am transforming the image into a byte array before transferring it. These are the methods found on the desktop application and on the J2ME respectively
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();
}
I am sending the message across a LAN, and I have no problems when I send short text messages instead of images. Also, I used wireshark and it seems that the desktop application is only sending part of the message. Any help would be highly appreciated. Also, everything works on the J2ME simulator.
请参阅官方原因“软件导致连接中止:套接字写入错误”
编辑
我认为一般来说没有什么可以说的,并且您的代码似乎没有任何异常之处导致连接中止。不过,我要注意的是:
write
调用将字节转换为整数是不必要的。它将自动升级。write(byte[])
而不是write(int)
会更好(更简单,在网络流量方面可能更有效)。Please refer to the answers to Official reasons for "Software caused connection abort: socket write error"
EDIT
I don't think there is much more that can be said in general, and there doesn't appear to be anything unusual about your code that would cause connections to abort. I would however note that:
write
call is unnecessary. It will be promoted automatically.write(byte[])
instead ofwrite(int)
.