title: Simple Java multi-threaded socket application
body: <p>I have this simple multi-threaded java socket application. Using classes Client.java</p>
  
  <pre><code>public class Client {
  
  private static Socket socket;
  private static boolean waitForServer = false;
  public static void main(String[] args) throws IOException {
      while(true){
          socket = new Socket("localhost", ServerPortInfo.getPort());
          PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
          BufferedReader bufferedReader = new java.io.BufferedReader(new InputStreamReader(System.in));
  
          while(true){
              PrintWriter.println(name + " Hello");
                  waitForServer = true;
  
              if (waitForServer){
                  BufferedReader inputBufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                  String inputString = null;
                  while((inputString = inputBufferedReader.readLine()) != null){
                      System.out.println(inputString);
                      break;
                  }
  
              }
  
          }
  
      }
    }
  }
  </code></pre>
  
  <p>And Server.java</p>
  
  <pre><code>public class Server {
  
  public static ArrayList&lt;ServerThread&gt; connections = new ArrayList&lt;ServerThread&gt;();
  
  public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {
      // Init server functionality
      new Server().runServer();
  
  
  }
  
  // Implementing server functionality
  public void runServer() throws IOException{
  
      ServerSocket serverSocket = new ServerSocket(ServerPortInfo.getPort());
      System.out.println("Server is running... Waiting for connections");
      while (true){
          Socket socket = serverSocket.accept();
          // After connection handle clients in threads
          ServerThread newThread = new ServerThread(socket);
          connections.add(newThread);
          newThread.start();
      }
  
  }
  }
  </code></pre>
  
  <p>And then a Thread that handles that connection...</p>
  
  <pre><code>public class ServerThread extends Thread {
  
  private static Socket socket;
  public static boolean alive = true;
  
  ServerThread(Socket socket){
      this.socket = socket;
  }
  
  public void run(){
      if (alive) {
          //DO STUFF NOW
  }       
  
  }
  </code></pre>
  
  <p>And now when I have multiple connections. It creates a different Thread for every connection. 
  What should i do when a user wants to disconnect from this server?</p>
  
  <p>Should i kill the thread, socket?</p>
  
  <p>I tried just saying to the thread 
  <code>alive = false;</code> so the run would just stop running. But this breaks other connections as well.</p>
  
  <p>EDIT</p>
  
  <p>ServerThread -></p>
  
  <pre><code>private Socket socket;
  </code></pre>
  
  <p>And a function -> </p>
  
  <pre><code>void closeConnection() throws IOException{
      this.socket.close();
  }
  </code></pre>
  
  <p>works like a charm.</p>