java.net.SocketException:软件导致连接中止:套接字写入错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2861533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:49:01  来源:igfitidea点击:

java.net.SocketException: Software caused connection abort: socket write error

javanetworkingjava-me

提问by npinti

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 桌面应用程序发送到 J2ME 应用程序。问题是我收到了这个异常:

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

我在网上查了一下,虽然这个问题并不罕见,但我找不到具体的解决方案。在传输图像之前,我正在将图像转换为字节数组。这些是分别在桌面应用程序和 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();
    }   

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.

我通过局域网发送消息,当我发送短文本消息而不是图像时,我没有问题。另外,我使用了wireshark,桌面应用程序似乎只发送了部分消息。任何帮助将不胜感激。此外,一切都适用于 J2ME 模拟器。

采纳答案by Stephen C

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:

我认为一般来说没有什么可以说的了,并且您的代码似乎没有任何不寻常的地方会导致连接中止。但是,我要注意的是:

  • Casting the bytes to integers for the writecall is unnecessary. It will be promoted automatically.
  • It would be better (simpler, potentially more efficient in terms of network traffic) to use write(byte[])instead of write(int).
  • The receiving side is assuming that each byte represents a complete character. This may be incorrect depending on how the sending side formed the bytes to be transmitted, and
  • It would be a good idea to start by sending a byte count so that the receiving end can tell if something has gone wrong before the sender sent the whole byte array.
  • write不需要为调用将字节转换为整数。它会自动升级。
  • 这将是更好(更简单,更可能在网络流量方面有效)的使用write(byte[])来代替write(int)
  • 接收方假设每个字节代表一个完整的字符。这可能是不正确的,具体取决于发送方如何形成要传输的字节,并且
  • 最好先发送字节计数,以便接收端可以在发送方发送整个字节数组之前判断是否出现问题。