Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 949
npinti Avatar asked May 18 '10 22:05

npinti


1 Answers

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 write call 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.
like image 200
Stephen C Avatar answered Oct 12 '22 19:10

Stephen C