title: Creating a Multi-threaded Java Server Chat App.
body: <p>I am trying to implement a multithreaded server chat application in Java.<br>
  This program created a thread and waits for a client to connect. Once a client is connected, it creates another thread and waits for another client to connect.  </p>
  
  <p>This is my <code>ChatServer.java</code>  </p>
  
  <pre><code>package com.chat.server;
  
  
  
  import java.io.InputStream;
  import java.io.IOException;
  import java.net.Socket;
  import java.net.InetAddress;
  import java.net.ServerSocket;
  
  
  
  /**
   * &lt;p&gt;The chat server program&lt;/p&gt;
   * This class is a Thread that recieves connection
   * from different clients and handles them is separate
   * Thread.
   *
   * @author Aditya R.Singh
   * @version 1.0.0
   * @since 1.0.0
   */
  class ChatServer extends Thread {
  
      private int port;                    // The port number to listen at.
      private String ip;                   // To store the IP address.
      private Socket socket;               // Socket connection with different clients.
      private InetAddress inet;            // Handling Client Address.
      private ServerSocket serverSocket;   // The server socket used by clients to connect.
  
  
  
      /**
       * This is solely intended for instantiation purpose.
       * 
       * @param PORT - The port number to listen for client requests
       */
      ChatServer(final int PORT) {
  
  
          /* Initiallizing all instance variables to null. */
          ip = null;
          inet = null;
          socket = null;
          serverSocket = null;
  
          /* Initiallizing the port number. */
          port = PORT;
      }
  
  
  
      /**
       * This method creates a connection between server and client.
       * 
       * @throws java.io.IOException
       */
      private void createConnection() throws IOException {
  
          serverSocket = new ServerSocket(port);  // Listen to the required port. 
          socket = serverSocket.accept();         // Accept the client connection.
      }
  
  
  
      /**
       * This method sets the IP address.
       */
      private void setIP() {
  
          inet = socket.getInetAddress();
          ip = new String(inet.getHostAddress());
      }
  
  
  
      /**
       * This method returns the IP address.
       *
       * @return IP address.
       */
      public String getIP() {
  
          return ip;
      }
  
  
  
      /**
       * This method checks if the socket has been connected
       * with any client.
       *
       * @return True if the client has been connected, else false
       */
      public boolean isConnected() {
  
          if(socket == null)
              return false;
          return true;
      }
  
  
  
      /**
       * This method returns the InputStream
       * from the Socket.
       * 
       * @return InputStream if Socket has been connected to the client, else null
       * @see java.io.InputStream
       */
      public InputStream getInputStream() throws IOException {
  
          if(socket == null)
              return null;
          return socket.getInputStream();
      }
  
  
  
      @Override
      public void run() {
  
          try {
  
              createConnection();
              setIP();
          } catch(IOException exception) {
  
              exception.printStackTrace();
          }
      }
  }  
  </code></pre>
  
  <p>And this is my <code>Server.java</code>:  </p>
  
  <pre><code>package com.chat.server;
  
  
  
  /** 
   * &lt;p&gt;The Server app&lt;/p&gt; 
   * This is the controller for accepting connections.
   *
   * @author Aditya R.Singh 
   * @version 1.0.0
   * @since 1.0.0
   */
  public class Server {
  
  
      /**
       * The port at which clients will connect.
       */
      public static final int PORT = 6005;          
  
  
  
      /**
       * For instantiation purpose.
       */
      public Server() {
  
  
      }
  
  
  
      public static void main(String[] args) {
  
  
          /* Keep accepting connections. */
          while(true) {
  
              ChatServer chat = new ChatServer(PORT); // Connecting port.
              chat.start();
  
              while(!chat.isConnected())
                  /* This is a false loop. Intended to keep running unless another client is not requesting to connect. */;
  
              System.out.println("We connected to: "+chat.getIP());   
          }
      }
  }  
  </code></pre>
  
  <p>The code compiles fine.<br>
  On running the code as:  </p>
  
  <pre><code>java com.chat.server.Server  
  </code></pre>
  
  <p>it seems that the program is listening for a client to connect. But after it connects to a client, it is expected to print the IP address of the client and then create another thread for another client. But it doesn't print the IP of the client.  </p>
  
  <p>This is my <code>Client.java</code>:  </p>
  
  <pre><code>package com.chat.client;
  
  
  
  import java.net.Socket;
  import java.io.IOException;
  
  
  
  public class Client {
  
      public static void main(String[] args) {
  
          Socket socket = null;
  
  
          try {
  
              socket = new Socket("127.0.0.1", 6005);
              System.out.println("Socket connected.");
          } catch(IOException ex) {
  
              ex.printStackTrace();
          }
      }
  }  
  </code></pre>
  
  <p>The client ones connects to the server, must print <code>Socket connected</code>. The client does that. The client works fine:  </p>
  
  <pre><code>java com.chat.client.Client  
  Socket connected.  
  </code></pre>
  
  <p>But the server app doesn't print the IP address of the client. Why so?</p>