title: how to create multiple clients with single server in java socket programming?
body: <p>I am trying to follow the Java socket programming.
  My goal is to create multiple client threads, which post their state
  and all those are listened by the same server.</p>
  
  <p>something like this
   1. Server Started
   2. Client A started
   3. Client B started
   4. Client A said "Hi" to Client B
   5. Client B said "Hi" to Client A
   6. Client A said "Hi2" to Client B
   7. Client B said "Hi2" to Client A
   8. Client A said "Hi3" to Client B
   9. Client B said "Hi3" to Client A
   10. Client A completed
   11. Client B completed</p>
  
  <p>what I have tried so far is :</p>
  
  <blockquote>
    <ol>
    <li>MyServer.java</li>
    </ol>
  </blockquote>
  
  <pre><code>import java.net.*;
  import java.io.*;
  
  class MyServer
  {
      public static void main(String args[]) throws Exception
      {
          ServerSocket server_socket = new ServerSocket(3333);
          Socket client_socket = server_socket.accept();
          DataInputStream din = new DataInputStream(client_socket.getInputStream());
          DataOutputStream dout = new DataOutputStream(client_socket.getOutputStream());
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  
          String str = "", str2 = "";
          int counter = 0;
          while (!str.equals("stop") &amp;&amp; counter &lt; 10)
          {
              str = din.readUTF();
              System.out.println("client says: " + str);
              //str2 = br.readLine();
              str2 = str +"server response ";
              dout.writeUTF(str2);
              dout.flush();
              counter ++;
          }
  
          din.close();
          client_socket.close();
          server_socket.close();
      }
  }
  </code></pre>
  
  <blockquote>
    <ol start="2">
    <li>MyClient.java</li>
    </ol>
  </blockquote>
  
  <pre><code>import java.util.stream.IntStream;
  
  public class ClientThread implements Runnable
  {
  String client_name;
  
  public ClientThread(String client_name)
  {
      this.setClient_name(client_name);
  }
  
  public void run()
  {
      for(int i=0;i&lt;5;i++)
      {
          System.out.println(this.getClient_name()+" : "+i);
          new ClientHelper().sendMessageToServer(this.getClient_name(),i);
          try
          {
              Thread.sleep(1000);
          } catch (InterruptedException e)
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  }
  
  public String getClient_name()
  {
      return client_name;
  }
  
  private void setClient_name(String client_name)
  {
      this.client_name = client_name;
  }
  </code></pre>
  
  <p>}</p>
  
  <blockquote>
    <p>3 . ClientHelper.java</p>
  </blockquote>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.DataInputStream;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.Socket;
  import java.net.UnknownHostException;
  
  public class ClientHelper
  {
      Socket socket;
      DataInputStream din ;
      DataOutputStream dout ;
  
      public void sendMessageToServer(String player_name, int i)
      {
          try
          {
              socket = new Socket("localhost", 3333);
              din = new DataInputStream(this.getSocket().getInputStream());
              dout = new DataOutputStream(this.getSocket().getOutputStream());
              // BufferedReader br = new BufferedReader(new
              // InputStreamReader(System.in));
              dout.writeUTF(player_name + " : says hi :" + i);
              dout.flush();
              socket.close();
          } catch (UnknownHostException e )
          {
              e.printStackTrace();
          } catch (IOException e)
          {
              e.printStackTrace();
          } finally
          {
          }
  
      }
  
      private Socket getSocket()
      {
          return socket;
      }
  
      private void setSocket(Socket socket)
      {
          this.socket = socket;
      }
  }
  </code></pre>
  
  <blockquote>
    <ol start="4">
    <li>App.java</li>
    </ol>
  </blockquote>
  
  <pre><code>public class App 
  {
  
      public static void main( String[] args )
      {
          new Thread(new ClientThread("A")).start();
          //new Thread(new ClientThread("B")).start();
          /*try
          {
  
              DataInputStream dataInputStream;
              DataOutputStream dataOutputStream;
              BufferedReader bufferedReader; 
              Socket socket = new Socket("localhost", 12345);
              dataInputStream = new DataInputStream(socket.getInputStream());
              dataOutputStream = new DataOutputStream(socket.getOutputStream());
              bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  
              Player p1 = new Player("p1", dataInputStream, dataOutputStream, bufferedReader);
              Player p2 = new Player("p2", dataInputStream, dataOutputStream, bufferedReader);
              p1.move();
              p2.move();
          } catch (UnknownHostException e)
          {
              e.printStackTrace();
          } catch (IOException e)
          {
              e.printStackTrace();
          }*/
  
      }
  }
  </code></pre>
  
  <p>It connects one client, but second client does not connect.</p>