title: Passing BufferedReader as a parameter of a constructor of a class
body: <p>I have set up a network and I've set up the reading and writing stream to a socket as so:</p>
  
  <pre><code>//Set up socket reads and writes
  final BufferedReader in = new BufferedReader(
      new InputStreamReader(client.getInputStream()));
  
  final PrintWriter out = new PrintWriter(
      client.getOutputStream(), true);
  </code></pre>
  
  <p>I wanted to pass the two variables, 'in' and 'out', as parameters of another class' constructor. This is how it looks in the other class</p>
  
  <pre><code>BufferedReader in;
  PrintWriter out;
  
  public ClientThread(BufferedReader in, PrintWriter out) {
      this.in = in;
      this.out = out;
  
  }
  </code></pre>
  
  <p>I then wanted to use those class variables to write to the output stream of the same socket like this (the class implements Runnable):</p>
  
  <pre><code>public void run() {
      while (true) {
          try {
              String userCommand = in.readLine();
          } catch (IOException e) {
              // Die if something goes wrong.
              System.err.println(e.toString());
              System.exit(1);
          }
      }
  }
  </code></pre>
  
  <p>However, whenever the code gets to this point, I get a SocketException:</p>
  
  <pre><code>java.net.SocketException: Socket closed
  </code></pre>
  
  <p>How can I fix this? I want to separate the setting up of the server and the socket from the processing of any commands given by the client.</p>
  
  <hr>
  
  <p>EDIT: Here's what the BufferedRead gets the input from</p>
  
  <pre><code>//create server socket
  ServerSocket server = new ServerSocket(portNum);
  
  // Accept a client if it appears
  Socket client = server.accept();
  </code></pre>
  
  <hr>
  
  <p>EDIT 2: I used these three files:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.Socket;
  import java.net.UnknownHostException;
  
  
  public class Client {
  
      public static void main(String[] args) throws UnknownHostException, IOException {
          //Change the socket if it doesn't work
          Socket sock = new Socket("localhost", 5920);
  
          //keyboard
          final BufferedReader stdin = new BufferedReader(
                  new InputStreamReader(System.in));
  
          //input from socket
          final BufferedReader in = new BufferedReader(
                  new InputStreamReader(sock.getInputStream()));
  
          //writer to socket
          final PrintWriter out = new PrintWriter(
                  sock.getOutputStream(), true);
  
          //new thread for incoming messages
          (new Thread(){
  
              @Override
              public void run() {
                  String serverMessage;
  
                  try {
                      while ((serverMessage = in.readLine()) != null) {
                          System.out.println(serverMessage);
  
                      }
                  } catch (IOException e) {
                      System.err.println("Something went wrong whilst trying "
                              + "to retrieve a message from the server");
                      System.exit(-1);
                  }
              }
          }).start();
  
          //new thread for outgoing messages
          (new Thread(){
  
              @Override
              public void run() {
                  String clientMessage;
  
                  try {
                      while ((clientMessage = stdin.readLine()) != null) {
                          out.println(clientMessage);
  
                      }
                  } catch (IOException e) {
                      System.err.println("Something went wrong whilst trying "
                              + "to send a message to the server.");
                      System.exit(-1);
                  }
  
              }
  
          }).start();
  
      }
  
  }
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  
  public class Server {
  
      public static void main(String[] args) throws IOException {
          ServerSocket server = new ServerSocket(5920);
  
          Socket client = server.accept();
  
          //Set up socket reads and writes
          final BufferedReader in = new BufferedReader(
                  new InputStreamReader(client.getInputStream()));
  
          final PrintWriter out = new PrintWriter(
                  client.getOutputStream(), true);
  
          new Thread(new ClassWithParam(in, out)).start();
  
      }
  
  }
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.PrintWriter;
  
  
  public class ClassWithParam implements Runnable {
  
      BufferedReader in;
      PrintWriter out;
  
      public ClassWithParam(BufferedReader in, PrintWriter out) {
          this.in = in;
          this.out = out;
  
      }
  
      @Override
      public void run() {
          while (true) {
              try {
                  System.out.println("HERE");
                  String userCommand = in.readLine();
                  System.out.println("HERE2");
              } catch (IOException e) {
                  // Die if something goes wrong.
                  System.err.println(e.toString());
                  System.exit(1);
              }
          }
  
      }
  
  }
  </code></pre>
  
  <p>And now it works. Don't know what happened. Will proceed to bang head against wall. Thanks.</p>