简体   繁体   中英

exception connection reset when exiting the program

I have a multithreaded server with a connection to MySQL and every time a I run it I get the same exception:java.net.SocketException: Connection reset this is my server: public void run(){ // synchronized(this){

        try {
            serverSocket = new ServerSocket(serverPort);
            System.out.println("Server started on port " + serverPort);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
         try{ 
             while(true){ 
                 Socket s = serverSocket.accept();
                 es.execute((Runnable) new WorkerThread(s));
                 }
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
             finally{ 

                     try {
                        serverSocket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                 }

}

this is my worker thread:

             String[] str = new String[10]; 
         String arr;
            try {
                int i=0;
                int b=0;
                String message=null;

            while(!Thread.currentThread().isInterrupted())   {
             message=in.readLine();

            if (message.equals("exit")){
            System.exit(0); 
            }
                       // .... here I have other if statements 
}

            } catch (IOException | SQLException e1) {

                e1.printStackTrace();
            } finally {
                try {
                    clientSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                }
            }

and the exception :

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.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Pack.WorkerThread.run(WorkerThread.java:62)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

it appears here:

message=in.readLine();

If you exit the program using System.exit(0); your connection will be terminated which leads to this exception.

If you do not want this exception to be thrown, make sure to close inputstream using in.close() prior to calling System.exit(0) or terminating the thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM