title: How to make server send a message to client on button click
body: <p>I have a client and a server. When client connects to server the server sends a message to the client.
  If the user clicks on button "New message" I want the server to send a new message to the client.
  How can I achieve that?</p>
  
  <p>My code (I have removed all try-Catch)</p>
  
  <p>Client:</p>
  
  <pre><code>private void getExpression() {
      Socket s = null;     
  
       s = new Socket(serverAddress, serverPort);                     
       out = new ObjectOutputStream(s.getOutputStream());             
       in = new ObjectInputStream(s.getInputStream());     
  
       while (true) {          
           Expression exp = (Expression) in.readObject();                    
           String message = exp.toString();
           expressionLabel.setText(message);  
        } 
      }
  
      public void newMessage() throws IOException {  
           //MAKE SERVER SEND NEW MESSAGE
      }
  
      @Override
      public void actionPerformed(ActionEvent ae) {
          if (ae.getSource().equals(newButton)) {
              newMessage();    
          }  
      }
  </code></pre>
  
  <p>Handler:</p>
  
  <pre><code>public class Handler extends Thread {    
      private Socket socket;              
      private String address;             
  
      public Handler (Socket s) {
          socket = s;
              address = socket.getInetAddress().getHostAddress();
      }
  
      @Override
      public void run() { 
          ObjectInputStream in = null;   
          ObjectOutputStream out = null;  
  
          out = new ObjectOutputStream(socket.getOutputStream());     
          in = new ObjectInputStream(socket.getInputStream());         
  
          out.writeObject(new Expression());               
          out.flush();
     }
  </code></pre>
  
  <p>Server:</p>
  
  <pre><code>private int port;
  
      public TestProgram(int port) {
          this.port = port;
          go();
      }
  
      public void go() {
          ServerSocket ss = null;    
          ss = new ServerSocket(port);   
  
          while (true) {
             Socket s = ss.accept();
             new Handler(s).start();                                 
          }
      }
  </code></pre>