0

Possible Duplicate:
java.net.SocketException: Software caused connection abort: recv failed

I'm trying to write a client server pair where the connection is live all day and the client waits for the server to send a message. The steps are:

  1. Server opens port and listens for connection
  2. Client connects in and waits for data
  3. Some time later (maybe hours) the server sends data to the client
  4. Client processes data and returns it to the server
  5. Repeat steps 3 and 4

I can get steps 1-4 working, however if I try to repeat step 3 I get the error in the title.

This is my method on the client side:

private static void waitForInput(SSLSocket sslsocket) throws IOException {
        do {
            try {
                ObjectInputStream ois = new ObjectInputStream(sslsocket.getInputStream());
                ObjectOutputStream oos = new ObjectOutputStream(sslsocket.getOutputStream());

                Object o = (Object) (ois.readObject());
                // excluded code to process data     

                oos.flush();
                oos.writeObject(o);
                oos.reset();
            }
            catch(ClassNotFoundException e){
                System.err.println(e.getMessage());
            }
        } while ( true );
    }

The code fails on the 4th line, The first time around it blocks and waits until I get the next bit of data, but it doesn't work twice. What am I doing wrong?

Community
  • 1
  • 1
Sarah
  • 245
  • 2
  • 6
  • 12
  • What is the "4th line"? And in what way does it fail? All you say is that it "doesn't work". – David Schwartz Sep 17 '12 at 20:22
  • Literally the 4th line in the code I have above `ObjectInputStream ois = new ObjectInputStream(sslsocket.getInputStream());` The error I get is the one in the title - 'java.net.SocketException: Software caused connection abort: recv failed' – Sarah Sep 17 '12 at 20:26

2 Answers2

0

Connection abort IOException is thrown when you are waiting to read from a socket that has been closed at the other end, check to see your server side code , if you are not accidentally closing the socket. Also the server side code could be uploaded for deeper analysis, also try posting the stack trace, it will help analyzing.

sharadendu sinha
  • 827
  • 4
  • 10
  • Ah you were right, I was resetting the connection on the other end, I need to just leave it active for the day. – Sarah Sep 17 '12 at 21:08
  • @Sarah No it isn't. It is caused by the local network stack in response to transmission errors. If the peer closes the connection you get an EOS from the API: in this case, an EOFException. Completely incorrect answer. -1. – user207421 Sep 17 '12 at 21:48
0

You could move the declaration for ois and oos out of the do ... while loop, since there is no need to redeclare them everytime, might need a try ... catch around that.

private static void waitForInput(SSLSocket sslsocket) throws IOException {
    ObjectInputStream ois = new ObjectInputStream(sslsocket.getInputStream());
    ObjectOutputStream oos = new ObjectOutputStream(sslsocket.getOutputStream());
    do {
        try {

            Object o = (Object) (ois.readObject());
            // excluded code to process data     

            oos.writeObject(o);
            oos.flush();
        }
        catch(ClassNotFoundException e){
            System.err.println(e.getMessage());
        }
    } while ( true );
}

And I have removed the oos.reset(); and moved the oos.flush();

I believe that the problem is the oos.reset(); and i would never reset a connection that is supposed to be persistent for hours, or, at least part of it.

Besides there is already a ois for that connection and you don't need two of them.

Ralph Andreasen
  • 103
  • 1
  • 1
  • 10