title: Threads on client server [java]
body: <p>I'm trying to do multi client server. I already setup server with multithreading. And my question is How Can I join(run) for example 20 clients at one time? I need to use threads aswell? I'm thinking about creating constructor with my client to set connection and get random name and then create loop which go through 20 of them and set them up?
  Here is the code:
  //Server</p>
  
  <pre><code>package com.multi;
  
  import java.io.IOException;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class Server {
  
      public static final int PORT = 4445;
  
  
      public void startServer(){
          try {
              ServerSocket server = new ServerSocket(PORT);
              System.out.println("Server is waiting for connetions...");
  
              while (true) {
                  Socket socket = server.accept();
                  new ServerThread(socket).start();
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
  
  
      public static void main(String[] args){
          new Server().startServer();
      }
  
  }
  </code></pre>
  
  <p>//server multithreading</p>
  
  <pre><code>package com.multi;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.Socket;
  
  public class ServerThread extends Thread {
      Socket socket;
      String message = null;
  
      ServerThread(Socket socket) {
          this.socket = socket;
      }
      public void run(){
          try {
              BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              while ((message = bufferedReader.readLine()) != null){
                  System.out.println("Client: " + message);
              }
              socket.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>
  
  <p>//client</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.Socket;
  import java.net.UnknownHostException;
  
  import javax.swing.JFrame;
  import javax.swing.JScrollPane;
  import javax.swing.JTextField;
  
  public class Client extends JFrame{
  
      private String[] names = { "Sara", "Jessica", "Tom", "Emanuel", "Joshua",
              "Kristin", "Kirito", "Goku", "Bulma" };
      private JTextField chatWindow;
  
      public void clientJoin() {
          for (String name : names) {
  
              try {
                  chatWindow = new JTextField();
                  add(new JScrollPane(chatWindow));
                  setSize(300, 150);
                  setVisible(true);
                  Socket socket = new Socket("localhost", 4445);
                  PrintWriter printWriter = new PrintWriter(
                          socket.getOutputStream(), true);
                  BufferedReader bufferedReader = new BufferedReader(
                          new InputStreamReader(System.in));
                  System.out.println(name);
                  while (true) {
                      String readerInput = bufferedReader.readLine();
                      printWriter.println(name + ": " + readerInput);
                      chatWindow.setText(name);
                  }
              } catch (UnknownHostException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      }
  
      public static void main(String[] args) {
  
          for (int i = 0; i &lt; 3; i++) {
              Runnable r = new Runnable() {
                  @Override
                  public void run() {
                      new Client().clientJoin();
                  }
              };
              new Thread(r).start();            
          }
  
      }
  
  }
  </code></pre>