title: SocketException: socket closed
body: <p>I am creating a Chat application with Java Sockets, and am getting the <code>SocketException: socket closed</code> when closing the server. I receive the error <strong>even if I never attempt to connect clients</strong>- <strong>just stopping the server</strong> will have this socket closed exception.</p>
  
  <p>I know what the error means, I'm trying to access the socket once it has already been closed, but I can't figure out why this would happen.</p>
  
  <pre><code>public ServerSocket server_socket;
  new ConnectionThread(server_back_end);
  
  class ConnectionThread extends Thread implements Runnable, Serializable
  {
      public ServerBackEnd server_back_end;
  
      public ConnectionThread(ServerBackEnd server_back_end) /*constructor*/
      {
          this.server_back_end = server_back_end;
          start();
      }
  
      @Override
      public void run()
      {
          try
          {
              server_back_end.server_socket = new ServerSocket(8080);
              server_back_end.server_front_end.display("&lt;&lt;SERVER STARTED&gt;&gt;");
  
              while (server_back_end.running)
                  add_incoming_clients();
          }
          catch (Exception e)
          {
              e.printStackTrace(); //this gets thrown when I stop server
          }
      }//end run()
  </code></pre>
  
  <p>I think the problem is in the add_incoming_clients(), because when I comment out the call, it does not throw the error. Even though I am not dealing with any clients, something happens at the <code>accept()</code> method:</p>
  
  <pre><code>    public void add_incoming_clients() throws IOException, ClassNotFoundException
      {
          Socket client_socket = server_back_end.server_socket.accept(); //line 172
  
          ClientInfo new_client = new ClientInfo(
                  new ObjectOutputStream(client_socket.getOutputStream()),
                  new ObjectInputStream(client_socket.getInputStream()));
       //...
       //...
      }
  </code></pre>
  
  <p>Here is the code when I stop the server::</p>
  
  <pre><code> public void stop()
      {
          if (running)
              try
              {
                  running = false;
                  server_socket.close();
                  server_front_end.display("&lt;&lt;SERVER TERMINATED&gt;&gt;");
              }
              catch (IOException e)
              {
                  System.out.println("stop server exception, is not the problem");
              }
      }//end stop()
  </code></pre>
  
  <p>Can someone explain what I must do to close it properly without an exception?
  Here is the error (line 172 is the culprit it seems, but I don't understand why)</p>
  
  <pre><code>java.net.SocketException: socket closed
      at java.net.DualStackPlainSocketImpl.accept0(Native Method)
      at java.net.DualStackPlainSocketImpl.socketAccept(DualStackPlainSocketImpl.java:131)
      at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409)
      at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:199)
      at java.net.ServerSocket.implAccept(ServerSocket.java:545)
      at java.net.ServerSocket.accept(ServerSocket.java:513)
      at ConnectionThread.add_incoming_clients(ServerBackEnd.java:172)
      at ConnectionThread.run(ServerBackEnd.java:159)
  </code></pre>