Simulating a connection reset in java

We start with a class that simply creates a server and a client. The server class is configured to accept a connection and then exit, forcing of course, all connections to terminate. The client class connects, then sleeps two seconds before attempting to read the stream from the now defunct server socket.

import java.net.*;
import java.io.*;

public class ConnectionResetPOC {
  public static void main(String[] args) throws Exception {
    ServerSocket socket = new ServerSocket(19999);
    System.out.println("Server Initialized");
    Socket connection = socket.accept();
    System.out.println("received request");
    BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
    InputStreamReader isr = new InputStreamReader(is);
    System.exit(1);
    connection.close();
  }
}

class ConnectionResetClient {
  public static void main(String argv[]) throws Exception {
    Socket s = new Socket("172.26.248.147", 19999);
    PrintWriter out = new PrintWriter(s.getOutputStream(), true);
    Thread.sleep(2000);
    out.write("foobar");
    System.out.println("wrote string");
    BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
    System.out.println(input.read());
  }
}

We run our server class…

c:\Users\showard>java ConnectionResetPOC
Server Initialized
received request

c:\Users\showard>

…and in another window run our client class. This run results in a connection reset exception being thrown back to the client…

c:\Users\showard>java ConnectionResetClient
wrote string
Exception in thread "main" java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(Unknown Source)
        at java.net.SocketInputStream.read(Unknown Source)
        at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
        at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
        at sun.nio.cs.StreamDecoder.read(Unknown Source)
        at java.io.InputStreamReader.read(Unknown Source)
        at java.io.BufferedReader.fill(Unknown Source)
        at java.io.BufferedReader.read(Unknown Source)
        at ConnectionResetClient.main(ConnectionResetPOC.java:26)

c:\Users\showard>

Below we show a wireshark trace, in which the port 19999 on the server host (the same as the client in our simple POC) is shown as sending two RST packets back to the client…

If needed, you can force network traffic to cross the network boundary on Windows with the following…

route add client_computer mask 255.255.255.255 network_gateway metric 1

If you would like to even further test this, you can use a python server as shown below and obtain the same result…

import socket, sys, random, struct

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = random.randint(3000,45000)
port = 19999
s.bind((socket.gethostname(), port))
s.listen(10)
print "started server on port ",port
while 1:
  conn, addr = s.accept()
  l_onoff = 1
  l_linger = 0
  conn.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,struct.pack('ii', l_onoff, l_linger))
  print 'Connected with ' + addr[0] + ':' + str(addr[1])
  #data = conn.recv(2048)
  data = "steve"
  if not data:
    break
  conn.sendall(data)
  conn.close()
  print "closed"
s.close()

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.