title: How to send message from server to client
body: <p>I am trying to make an chatprogram in Java, when I send message on the client side the server side gets the message. But when I send from the server side to the client it do not get the message.</p>
  
  <p>I cannot see what I am doing wrong.</p>
  
  <p>The server side code: </p>
  
  <pre><code>private void serverStart(){
      textArea.append("Starting server " + " \n");
  
      try {
          serverSocket = new ServerSocket(4444);
          textArea.append("Waiting for Clients " + " \n");
  
          //Reading message from the client
          socket = serverSocket.accept();
  
          textArea.append("Client Connected " + "\n");
  
          //Send message to client 
          out = new PrintWriter(socket.getOutputStream());
          in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
          while (true)
          {        
  
              messageFromClient = in.readLine();
              whileChat(messageFromClient); 
          }
      } catch(IOException ioExecption) {
          ioExecption.printStackTrace();
      }
  }
  
  private void whileChat(String messageFromClient) {
      showMessage(messageFromClient);
      System.out.println("Message from client : " + messageFromClient);
  }
  
  protected static void showMessage(final String message) {
      SwingUtilities.invokeLater( 
      new Runnable(){
          public void run()
          {
              Gui.consoleTextArea.append(message + "\n");
          }
      });
  }
  
  public static void sendMessage(String message) {
      out.println(message);
      showMessage(name +  " : " + message + "\n");
  }
  </code></pre>
  
  <p>The Client side : </p>
  
  <pre><code>private void connectToServer() {
  
      try {
          socket = new Socket("localhost", 4444);
      } catch (UnknownHostException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
      } catch (IOException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
      }
  
      Thread clientThread = new Thread(new Runnable() {
          @Override
          public void run() {
              try {
                  // attach to socket's output stream with auto flush turned on
                  //Send message to the server
                  out = new PrintWriter(socket.getOutputStream(),
                          true);
  
                  //Get return message from server
                  in = new BufferedReader(new InputStreamReader(
                          socket.getInputStream()));
                  messageFromServer = in.readLine();
                  whileChatting(messageFromServer);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      });
      clientThread.start();
  }
  
  private void whileChatting(String messageFromServer) {
      showMessage(messageFromServer);
      System.out.println("Message from server to client " + messageFromServer);
  }
  
  public static void Send(String msg) {
      out.println(name + " : " + msg);
      showMessage(name +  " : " + msg + "\n");
  }
  protected static void showMessage(final String message) {
      SwingUtilities.invokeLater( 
      new Runnable(){
          public void run(){
              Gui.consoleTextArea.append(message);
          }
      });
  }
  </code></pre>
  
  <p>Hope someone could help me with this problem.</p>