title: Java Client/Server resource leak after closing
body: <p>I am trying to implement a java command line chat server and client. I am using 4 classes, the Server.java sets up a ServerSocket and accepts all connections. The Client.java can connect to the Server, then 2 threads for both client and server are created using the SendMessages.java and ReceiveMessages.java. The threads are responsible for taking the input from stdin and sending it to the output stream of the socket, and taking the incoming input stream to print it to stdout. Everything works when launching the server and connecting the client, the chat also works. However, when either client or server is terminated a resource leak is caused. I want the SendMessages class and ReceiveMessages class to be able to detect when the connection of client or server is terminated and close all resources to avoid resource leaks. Here is the code:</p>
  
  <blockquote>
    <p>Server.java</p>
  </blockquote>
  
  <pre><code>import java.net.*;
  import java.io.*;
  import java.util.*;
  public class Server{
  public static void main(String[] args) throws SocketException,IOException{
      ServerSocket s = new ServerSocket(8000);
      s.setSoTimeout(10000000);
      while(true){
          Socket clientSocket = s.accept();
      handle(clientSocket);
  
  }  
  }
  
     static void handle(Socket clientSocket) throws IOException{
      System.out.println("connection accepted from " + clientSocket.getRemoteSocketAddress());
      receiveMessages(clientSocket);
      sendMessages(clientSocket);
    }
  
    static void receiveMessages(Socket clientSocket) throws IOException{
      (new Thread(new ReceiveMessages(clientSocket))).start();;
  
    }
  
    static void sendMessages(Socket clientSocket)throws IOException{
      (new Thread(new SendMessages(clientSocket))).start();
    }
  }
  </code></pre>
  
  <blockquote>
    <p>Client.java</p>
  </blockquote>
  
  <pre><code>import java.net.*;
  import java.io.*;
  import java.util.*;
  
  public class Client{
  
  
  public static void main(String[] args) throws IOException, UnknownHostException, ConnectException{
      String hostname = args[0];
      int port = Integer.parseInt(args[1]);
      Socket s = null;
      s = connect(hostname, port);
      handle(s);
  }
  
  
  public static Socket connect(String hostname, int port) throws UnknownHostException, IOException, ConnectException{
      Socket s = null;
      try{
      s = new Socket(hostname, port);
      } catch(ConnectException e){
          System.out.println("Connect Exception caught!");
      }
  return s;
  }
  
  static void handle(Socket clientSocket) throws IOException, UnknownHostException{
  receiveMessages(clientSocket);
  sendMessages(clientSocket);
  }
  
  static void receiveMessages(Socket clientSocket) throws IOException, NullPointerException{
      (new Thread(new ReceiveMessages(clientSocket))).start();
    }
  
  static void sendMessages(Socket clientSocket)throws IOException, NullPointerException{
    (new Thread(new SendMessages(clientSocket))).start();
  }
  
  }
  </code></pre>
  
  <blockquote>
    <p>SendMessages.java</p>
  </blockquote>
  
  <pre><code>import java.io.*;
  import java.net.*;
  import java.util.*;
  public class SendMessages implements Runnable{
  Socket clientSocket;
  
  public SendMessages(Socket clientSocket){
      this.clientSocket = clientSocket;
  }
  
  public void run(){
      System.out.println("SendMessages thread has started.");
      Scanner sc = new Scanner(System.in);
      PrintWriter out = null;
      try{
      out = new PrintWriter(clientSocket.getOutputStream(), true);
      }
      catch(IOException | NullPointerException e){}
  
      String message;
  
      while(true){
          message = sc.nextLine();
          out.println(message);
          if(out.checkError()){
              out.close();
              sc.close();
              System.out.println("SendMessages closed");
          }
      }
  
  }
  }
  </code></pre>
  
  <blockquote>
    <p>ReceiveMessages.java</p>
  </blockquote>
  
  <pre><code>import java.io.*;
  import java.net.*;
  import java.util.*;
  public class ReceiveMessages implements Runnable{
      Socket clientSocket;
      public ReceiveMessages(Socket clientSocket){
          this.clientSocket = clientSocket;
      }
      public void run(){
          System.out.println("ReceiveMessages thread has started.");
          BufferedReader in = null;
          try{
          in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
          }catch(IOException|NullPointerException e){}
          String message;
          try{
              while(true){
              while((message = in.readLine())!= null){
                  System.out.println(message);
              }
  
      }
      } catch(IOException|NullPointerException e){
          System.out.println("ReceiveMessages resources closed.");
          try{  
          in.close();
              clientSocket.close();
              }
              catch(IOException f){}
      }
  }
  }
  </code></pre>
  
  <p>Thank you!</p>