Connection Reset – What does it mean?

Generally, it indicates your client successfully connected to the server. A subsequent attempt to use the client connection failed after the server closed the socket, or had been disconnected for any reason.

You can prove this with what is below, after starting the serverSocket in another window…

c:\Users\showard>type serverSocket.java
import java.net.*;
import java.io.*;

class serverSocket {
  public static void main (String args[]) throws Exception {
    ServerSocket ss;
    ss = new ServerSocket(5000);
    Socket clientSocket = ss.accept();
    Thread.sleep(20000);
  }
}

class clientSocket {
  public static void main (String args[]) throws Exception {
    Socket s = new Socket("localhost", 5000);
    DataInputStream input = null;
    try {
      input = new DataInputStream(s.getInputStream());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    Thread.sleep(10000);
    byte[] b = new byte[input.readInt()];
  }
}

c:\Users\showard>java clientSocket
Exception in thread "main" java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(Unknown Source)
        at java.net.SocketInputStream.read(Unknown Source)
        at java.net.SocketInputStream.read(Unknown Source)
        at java.io.DataInputStream.readInt(Unknown Source)
        at clientSocket.main(serverSocket.java:24)

c:\Users\showard>cd c:\Users\showard

It should be noted that this depends on the OS on which the JVM runs. For example, the code above will throw a java.io.EOFException. It all depends on what exception the OS hands back to the JVM, which then translates it. On Linux, the recvfrom() system call on the socket with no one on the other end resulted in the following:

recvfrom(6, "", 1, 0, NULL, NULL) = 0

a “man recvfrom” will show the following..

ssize_t recvfrom(int s, void *buf, size_t len, int flags,
                        struct sockaddr *from, socklen_t *fromlen);

As such, our failed call used file descriptor 6, had a zero length buffer, the size was 1, flags were 0, and the from socket address and from length were each null. The value of 0 for the recvfrom() return value indicates the peer had an orderly shutdown, which it did.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.