title: How to reproduce "java.net.SocketException.Connection reset"?
body: <p>I am trying to reproduce "java.net.SocketException.Connection reset" exception. </p>
  
  <p>Wanted to know if there is any program available which could help me simulate it. I tried following Server and client programs to see if I could simulate but I am not able to get any exception. I am using java8.</p>
  
  <p>Server Code-</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.net.SocketException;
  import java.net.SocketTimeoutException;
  
  
  public class SimpleServerApp {
  
      public static void main(String[] args) throws InterruptedException {
  
          new Thread(new SimpleServer()).start();
  
      }
  
      static class SimpleServer implements Runnable {
  
          @Override
          public void run() {
  
              ServerSocket serverSocket = null;
  
              try {
                  serverSocket = new ServerSocket(3333);
                  serverSocket.setSoTimeout(0);
  
                  //serverSocket.
                  while (true) {
                      try {
                          Socket clientSocket = serverSocket.accept();
  
                          BufferedReader inputReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  
                          System.out.println("Client said :"+ inputReader.readLine());
  
                      } catch (SocketTimeoutException e) {
                          e.printStackTrace();
                      }
                  }
  
              }catch (Exception e) {
                  e.printStackTrace();
                  System.out.println(" EXCEPTION " + e.getStackTrace());
              }/*catch (IOException e1) {
                  e1.printStackTrace();
              }*/ /*finally {
                  try {
                      if (serverSocket != null) {
                          serverSocket.close();
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }*/
  
          }
  
      }
  }
  </code></pre>
  
  <p>Client Code - </p>
  
  <pre><code>import java.io.IOException;
  import java.io.PrintWriter;
  import java.net.Socket;
  import java.net.SocketException;
  import java.net.UnknownHostException;
  
  
  public class SimpleClientApp {
  
      public static void main(String[] args) {
  
          new Thread(new SimpleClient()).start();
  
      }
  
      static class SimpleClient implements Runnable {
  
          @Override
          public void run() {
  
              Socket socket = null;
              try {
  
                  socket = new Socket("localhost", 3333);
  
  
                  PrintWriter outWriter = new PrintWriter(socket.getOutputStream(), true);
  
                  System.out.println("Wait");
  
                  Thread.sleep(20000);
                  //System.exit(0);
                  //throw new Exception("Random exception");
                  //socket.close();
  
                  outWriter.println("Hello Mr. Server!");
  
              }catch (SocketException e) {
                  e.printStackTrace();
              }catch (InterruptedException e) {
                  e.printStackTrace();
              } catch (UnknownHostException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              } catch (Exception e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } /*finally {
  
                  try {
                      if (socket != null)
                          socket.close();
                  } catch (IOException e) {
  
                      e.printStackTrace();
                  }
  
  
              }
  */      }
  
      }
  
  }
  </code></pre>
  
  <p>Scenario 1. </p>
  
  <ul>
  <li>Start the server program locally.  </li>
  <li>Start the Client program locally.</li>
  <li>Close the client program abruptly (Ctrl C) - I just get output on
  Server program "Client said :null"</li>
  </ul>
  
  <p>Scenario 2.</p>
  
  <ul>
  <li>Start the Server program Locally.</li>
  <li>Start the client program locally.</li>
  <li>Client is connected to server, Then while client program is waiting close the server program abruptly. Still no exception.</li>
  </ul>
  
  <p>Can some tell me some way I could produce the connection reset exception, With working sample code.  </p>