0 Replies Latest reply: Dec 4, 2006 9:36 AM by 807607 RSS

    Closing socket on server doesn't affect client socket

    807607
      Hi,
      i'm trying to understand why when i close the server socket, client that is writing to it doesn't thorw any exceptions..
      SocketServer s = new ServerSocket(7000);
      ClientThread client = new ClientThread(s.accept());
      client.start();
      When a client connect on that port a ClientThread object is created.
      On ClientThread i get the socket in and out streams:
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream());
      The client class that i use to test it is:
      public class TestClient {
      
        public static void main(String[] args) {
          Socket socket = null;
          BufferedReader in = null;
          PrintWriter out = null;
          // Message terminator
          char EOF = (char) 0x00;
          try {
            String ip = "";
            ip = "127.0.0.1";
            socket = new Socket(ip, 7000);
            System.out.println("Connecting to " + ip + ":7000");
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream());
      
            // send lin
            System.out.println("sending");
            out.println("#LOGIN:user,pass" + EOF);
            out.flush();
      
            // read
            System.out.println("reading");
            System.out.println(in.readLine());
            try {
              out.println("#SAY:hello" + EOF);
              out.flush();
              System.out.println("hello");
              Thread.sleep(700);
              out.println("#SAY:hello2" + EOF);
              out.flush();
              System.out.println("hello2");
              Thread.sleep(700);
              out.println("#SAY:hello3" + EOF);
              out.flush();
              System.out.println("hello3");
              Thread.sleep(700);
              out.println("#SAY:hello4" + EOF);
              out.flush();
              System.out.println("hello4");
              Thread.sleep(700);
              out.println("#SAY:hello5" + EOF);
              out.flush();
              System.out.println("hello5");
              Thread.sleep(700);
              out.println("#SAY:hello6" + EOF);
              out.flush();
              System.out.println("hello6");
              Thread.sleep(600);
              out.println("#SAY:hello7" + EOF);
              out.flush();
              System.out.println("hello7");
              Thread.sleep(5000);
              out.println("#SAY:hello" + EOF);
              out.flush();
              System.out.println("hello8");
              Thread.sleep(5000);
              out.println("#SAY:hello9" + EOF);
              out.flush();
              System.out.println("hello9");
              Thread.sleep(5000);
              out.println("#SAY:hello10" + EOF);
              out.flush();
              System.out.println("hello10");
            }
            catch (InterruptedException ex) {
            System.out.println(ex.getMessage());
            }
            out.close();
          }
          catch (IOException e) {
            System.out.println(e);
          }
        }
      }
      CLientThread receive correctly the output streams.
      After the hello6 or hello7 the socket in ClientThread get closed (socket.close()) to emulate the flooding.
      But the TestClient doesn't throw any exceptions and keep sending the out.println("hellos").
      After TestClient send the hello10, then it exit normally.
      If the socket on the server that is the result of serversocket.accept() is closed, shouldn't the client throw any exceptions?

      Thanks in advance.