javajava-8socketexception

How to reproduce "java.net.SocketException.Connection reset"?


I am trying to reproduce "java.net.SocketException.Connection reset" exception.

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.

Server 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();
                }
            }*/

        }

    }
}

Client 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();
                }


            }
*/      }

    }

}

Scenario 1.

Scenario 2.

Can some tell me some way I could produce the connection reset exception, With working sample code.


Solution

  • This works for me:

    class Server {
    
        public static void main(String[] args) throws Exception {
            ServerSocket ss = new ServerSocket(9999);
            Socket s = ss.accept();
            InputStream i = s.getInputStream();
            i.read();
        }
    }
    

    client connects and disconnects without closing socket

    class Client {
    
        public static void main(String[] args) throws Exception {
            Socket s = new Socket("localhost", 9999);
        }
    }
    

    this results in exception on server

    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 net.Server.main(Server.java:13)