title: Socket closed as soon as passed into new thread
body: <p>I am trying to create a multi-threaded duplex chat server. I had it working fine before I moved the code into threads, but now I'm getting "SocketException: Socket is closed" whenever a thread tries to access a socket. </p>
  
  <p>My teacher and I can't figure it out. (teacher knows even less Java than I do; he is a C guy). It seems like the socket is open, but as soon as it goes into the thread, it is closed.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>Client code:</p>
  
  <pre><code>public class Client {
      public static void main(String[] args) throws IOException {
          String hostName = "localhost";
          int portNumber = 6969;
          try (
                  Socket socket = new Socket(hostName, portNumber);
          ) {
              System.out.println("Chat connected");
              //Sender
              if (!socket.isClosed())
                  new Thread(new Sender(socket)).start();
              //Receiver
              if (!socket.isClosed())
                  new Thread(new Receiver(socket)).start();
          } catch (SocketException e) {
              System.out.println("Connection terminated unexpectedly");
          }
      }
  }
  </code></pre>
  
  <p>Server code:</p>
  
  <pre><code>public class Server {
      public static void main(String[] args) throws IOException {
          int portNumber = 6969;
          try (
                  ServerSocket serverSocket = new ServerSocket(portNumber);
                  Socket clientSocket = serverSocket.accept();
          ) {
              //Receiver
              if (!clientSocket.isClosed())
                  new Thread(new Receiver(clientSocket)).start();
              //Sender
              if (!clientSocket.isClosed())
                  new Thread(new Sender(clientSocket)).start();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Receiver thread code:</p>
  
  <pre><code>class Receiver implements Runnable {
  
      private Socket socket;
  
      Receiver(Socket s) throws IOException {
          socket = s;
      }
  
      @Override
      public void run() {
          try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
              String received;
              do {
                  received = in.readLine();
                  if (received == null) break;
                  System.out.println("Them: " + received);
              } while (!received.contains("/dropmic"));
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Sender thread code:</p>
  
  <pre><code>class Sender implements Runnable {
      private Socket socket;
  
      Sender(Socket s) throws IOException {
          socket = s;
      }
  
      @Override
      public void run() {
          try (PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
               BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {
              String sent;
              do {
                  System.out.print("You: ");
                  sent = stdIn.readLine();
                  if (sent == null) break;
                  out.println(sent);
              } while (!sent.contains("/dropmic"));
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Server output:</p>
  
  <pre><code>java.net.SocketException: Socket is closed
      at java.net.Socket.getOutputStream(Socket.java:943)
      at Sender.run(Sender.java:16)
      at java.lang.Thread.run(Thread.java:745)
  java.net.SocketException: Socket closed
      at java.net.SocketInputStream.socketRead0(Native Method)
      at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
      at java.net.SocketInputStream.read(SocketInputStream.java:170)
      at java.net.SocketInputStream.read(SocketInputStream.java:141)
      at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
      at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
      at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
      at java.io.InputStreamReader.read(InputStreamReader.java:184)
      at java.io.BufferedReader.fill(BufferedReader.java:161)
      at java.io.BufferedReader.readLine(BufferedReader.java:324)
      at java.io.BufferedReader.readLine(BufferedReader.java:389)
      at Receiver.run(Receiver.java:19)
      at java.lang.Thread.run(Thread.java:745)
  
  Process finished with exit code 0
  </code></pre>
  
  <p>Client output (Still running):</p>
  
  <pre><code>Chat connected
  You: java.net.SocketException: Socket is closed
      at java.net.Socket.getInputStream(Socket.java:903)
      at Receiver.run(Receiver.java:16)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>