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

Posted by npinti on Stack Overflow See other posts from Stack Overflow or by npinti
Published on 2010-05-18T22:15:29Z Indexed on 2010/05/18 22:20 UTC
Read the original article Hit count: 363

Filed under:
|
|

Hi guys,

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.

Thanks.

© Stack Overflow or respective owner

Related posts about java

Related posts about j2me