title: multiclient java client server chat
body: <p>I'm tryinna write a multiple client server chat. Everything should run on the same computer, each client in the different shell terminal console. I read couple of threads on this side, where people said, that I should run new thread on server for every single connected client. So that's what I've done.Still,Server is printing messages  from every client on its console but clients can see only messages they wrote, not every message submitted on chat. What should I do to fix it?</p>
  
  <p>Here is code:
  Server:</p>
  
  <pre><code>//package javauj.serwer;
  import java.io.IOException;
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.Calendar;
  
  
  public class Server {
      private static final int PORT_NUM = 5051;
      private static ServerSocket serS;
      private static Socket s;
  
      public static void main(String[] args) throws IOException{
          DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
          Calendar cal = Calendar.getInstance();
              System.out.println(df.format(cal.getTime())+" Server waiting on port "+PORT_NUM);
              try {
                  serS = new ServerSocket(PORT_NUM);
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
  
  
          while(true){
              try {
                  s = serS.accept();
                  System.out.println("New user connected");
                      ServerThread st=new ServerThread(s);
                          st.start();
  
  
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }   
  
  
  
  
  
          }
      }
  }   
  </code></pre>
  
  <p>Server Thread:</p>
  
  <pre><code>import java.io.IOException;
  import java.io.PrintStream;
  import java.net.Socket;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.Calendar;
  import java.util.Scanner;
  
  
  public class ServerThread extends Thread {
      Socket socket=null;
      private Scanner sc;
      private PrintStream ps;
      public ServerThread(Socket s) {
          this.socket=s;
  
      }
  
  
  
          public void run(){
              DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
              Calendar cal = Calendar.getInstance();
              String response;    
                  while(true){    
                  try {
                          sc = new Scanner(socket.getInputStream());
                          ps = new PrintStream(socket.getOutputStream());
                  } catch (IOException e) {
                              // TODO Auto-generated catch block
                          e.printStackTrace();
                  }
  
                  if((response=sc.nextLine())!=null){
  
  
                      System.out.println(df.format(cal.getTime())+" Napisal "+response);
  
                      ps.println(df.format(cal.getTime())+" Napisal :"+response);
  
                      }   
              }
  
  
          }
  
  
  }
  </code></pre>
  
  <p>and Client</p>
  
  <pre><code>import java.io.IOException;
  import java.io.PrintStream;
  import java.net.Socket;
  import java.net.UnknownHostException;
  import java.util.Scanner;
  
  
  
  
  public class Client {
      static Scanner sc;
      static Scanner sc2;
      static Socket s;
      static String response;
      public static void main(String[]args) throws UnknownHostException, IOException{
          String send;
          sc=new Scanner(System.in);  //client send scanner
          s = new Socket("127.0.0.1", 5051);
          sc2 = new Scanner(s.getInputStream());  //client response scanner
          PrintStream ps = new PrintStream(s.getOutputStream());
  
          while(true){
  
              System.out.println("&gt;");    
              send = sc.nextLine();
  
              ps.println(send);       
              // I tried run new thread for responses
              //Thread thread = new Thread(new Runnable(){
                  //public void run(){
                      //while(true){
                          response=sc2.nextLine();        
                          System.out.println(response);
                  //  }   
                  //}
              //});
          }
  
      }
  }
  </code></pre>