title: Multi-Threading Server/Client Chat
body: <p>So this is my first post here, I am currently trying to make a Java client/server chat application using socket programming.</p>
  
  <p>I currently have the server waiting for a client to connect and then passing the client's messages back to the client. So far I have tried different methods to make the server continuously listen to new clients and connect them allowing them to post and view messages with each other. </p>
  
  <p>Can you point me in the right direction and how I should implement this?</p>
  
  <hr>
  
  <h2>SERVER</h2>
  
  <blockquote>
    <p></p>
  </blockquote>
  
  <pre><code>class TCPServer {
  private ServerSocket serverSocket;
  private int port;
  String newLine = System.getProperty("line.separator");
  public TCPServer(int port) {
      this.port = port;
  }
  public void begin() throws IOException {
  
      System.out.println("Starting the server at port: " + port);
      serverSocket = new ServerSocket(port);
  
      System.out.println("Waiting for clients... " + newLine);
  
      try {
  
          Socket connectionSocket = serverSocket.accept();
  
          DataOutputStream hello =
                  new DataOutputStream(connectionSocket.getOutputStream());
          hello.writeUTF("You have successfully connected!" + newLine + "please type your      username");
  
  
          //A client has connected to this server. Get client's username
          String username = getUserName(connectionSocket);
          System.out.println(username + " has connected" + newLine);
          //Start chat method
          startChat(connectionSocket, username);
      } catch (Exception e) {
      }
  
  }
  
  public String getUserName(Socket connectionSocket) throws IOException {
      String clientUserName;
      // ArrayList clients = new ArrayList();
  
  
      BufferedReader userNameClient =
              new BufferedReader(new InputStreamReader(
              connectionSocket.getInputStream()));
  
  
      clientUserName = userNameClient.readLine();
      //clients.add(clientUserName);
  
  
      DataOutputStream greetingFromServer =
              new DataOutputStream(connectionSocket.getOutputStream());
      //writeUTF Caused incorrect key codes to appear at beginning of String
      greetingFromServer.writeBytes("Welcome " + clientUserName + ", please type your message" + newLine);
  
  
      return clientUserName;
  }
  
  public void startChat(Socket connectionSocket, String username) throws IOException {
      String clientSentence;
      String clientMessageOut;
  
  
  
      while (true) {
          //Socket connectionSocket = welcomeSocket.accept();
  
          BufferedReader inFromClient =
                  new BufferedReader(new InputStreamReader(
                  connectionSocket.getInputStream()));
          DataOutputStream outToClient =
                  new DataOutputStream(connectionSocket.getOutputStream());
  
  
  
          //Loops to check if client message is not empty
          while ((clientSentence = inFromClient.readLine()) != null) {
              outToClient.writeBytes(username + ": " + clientSentence + newLine);
              if (clientSentence.equals("close")) {
                  System.out.println(username + " has left the server");
              }
  
          }
      }
  }
  
  public static void main(String[] args) throws Exception {
      int port = 6788;
  
      TCPServer welcomeSocket = new TCPServer(port);
      welcomeSocket.begin();
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <hr>
  
  <h2>CLIENT</h2>
  
  <blockquote>
    <p></p>
  </blockquote>
  
  <pre><code>class TCPClient {
  
  public static void main(String[] args) throws Exception {
      String sentence;
      String modifiedSentence;
      boolean keepConnection = true;
      int port = 6788;
      Scanner inFromUser = new Scanner(System.in);
  
      Socket clientSocket = new Socket("localhost", 6788);
      //System.out.println("You are connented to server"+ '\n'+"Please enter your username: ");
      String newLine = System.getProperty("line.separator");
  
      //Recieve greeting message
      InputStream messageFromServer = clientSocket.getInputStream();
      DataInputStream in =
              new DataInputStream(messageFromServer);
      System.out.println("FROM SERVER: " + in.readUTF());
  
  
      DataOutputStream outToServer =
              new DataOutputStream(clientSocket.getOutputStream());
      BufferedReader inFromServer =
              new BufferedReader(new InputStreamReader(
              clientSocket.getInputStream()));
  
      while ((sentence = inFromUser.nextLine()) != null) {
          outToServer.writeBytes(sentence + '\n');
          modifiedSentence = inFromServer.readLine();
          System.out.println("sentence = " + sentence);
          System.out.println(modifiedSentence);
          if (sentence.equals("close")) {
              System.out.println("You have left the server");
              outToServer.writeBytes("close" + newLine);
              break;
          }
      }
      clientSocket.close();
  }
  </code></pre>
  
  <p>}</p>