简体   繁体   中英

(Java Multiclient server) No exception when client closes socket and server writes to client

Server code:

public class Main {
    public static void main(String args[]){
        Socket s=null;
        ServerSocket ss2=null;
        System.out.println("Server Listening......");
        try{
            ss2 = new ServerSocket(8080);
        }
        catch(IOException e){
            e.printStackTrace();
            System.out.println("Server error");
        }

        while(true){
            try{
                s = ss2.accept();
                ss2.setReuseAddress(true);
                System.out.println("connection Established");
                ServerThread st=new ServerThread(s);
                st.start();
            }
            catch(Exception e){
                e.printStackTrace();
                System.out.println("Connection Error");
            }
        }
    }
}

class ServerThread extends Thread{  

    String line; = "testing";
    PrintWriter os=null;
    Socket s=null;

    public ServerThread(Socket s){
        this.s=s;
    }

    public void run() {
        try{
            os=new PrintWriter(s.getOutputStream());

        }catch(IOException e){
            System.out.println("IO error in server thread");
        }

        try {
            for (int i = 0 ; i < 100 ; i++){
                os.println(line);
                os.flush();
                Thread.sleep(1000);
            }
        }
        catch(NullPointerException | IOException | InterruptedException e){
            System.out.println("Client "+line+" Closed");
        }

        finally{    
            try{
                System.out.println("Connection Closing..");

                if(os!=null){
                    os.close();
                    System.out.println("Socket Out Closed");
                }
                if (s!=null){
                s.close();
                System.out.println("Socket Closed");
                }

                }
            catch(IOException ie){
                System.out.println("Socket Close Error");
            }
        }//end finally
    }

When a client connects to the server, the server starts to println. But when the client closes socket, the server still keeps println. Suppose the server will throw exception?

Any help would be appreciated.

  1. PrintWriter swallows exceptions. See the Javadoc.
  2. You will never get an exception on the first write to a connection that has been closed by the peer, for buffering reasons. You will eventually get a 'connection reset'. But never on the first write.
  3. Calling setReuseAddress() after the ServerSocket has been bound is completely pointless, as is calling it inside the loop.

I think you should add ss2.close() and s.close() when you finish the while

while(true){
    try{
    s = ss2.accept();
    ss2.setReuseAddress(true);
    System.out.println("connection Established");
    ServerThread st=new ServerThread(s);
    st.start();
    }
    catch(Exception e){
        e.printStackTrace();
        System.out.println("Connection Error");
     }
 }
 ss2.close();
 s.close();

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