views:

746

answers:

1

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.

A: 

Please refer to the answers to http://stackoverflow.com/questions/2126607/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.
Stephen C
I had already gone through those answers. I looked at the windows event log, I did not see anything relevant, event though I am not that experienced at going through these logs.Secondly, I also tried adding in the shutdownOutput(); but still no avail.Lastly, I get an error from the J2ME application saying that the socket was closed.
npinti
Well... I do not know how but I seem to can get it working now. I tried replacing the casting to integer part, but that totally broke down my application, disabling it completely. I tried sending it today and it worked. My guess is that it was some external cause, probably the router restarted or something else.
npinti