title: Socket program to send and receive user defined objects not working
body: <p>I have a user defined class Message, whose object I would like to pass between the client and the server.</p>
  
  <p>The Message class is as follows:</p>
  
  <pre><code>import java.io.Serializable;
  
  public class Message implements Serializable
  {
      String CorS;
      int data_id;
      int status_id;
      Integer value;
      boolean withdraw;
  
      public Message()
      {
          CorS = null;
          data_id = 0;
          status_id = 0;
          value = 0;
          withdraw = false;
      }
  
      public Message(String CorS, int data_id, int status_id, Integer value)
      {
          this.CorS = CorS;
          this.data_id = data_id;
          this.status_id = status_id;
          this.value = value;
      }
  
      public Message(boolean withdraw)
      {
          this.withdraw = withdraw;
      }
  }
  </code></pre>
  
  <p>The code in the client side which sends the object to the server is as follows:</p>
  
  <pre><code>Socket s = null;
      ObjectInputStream in = null;
      ObjectOutputStream out = null;
  
      String hostname = null;
      int port_no = 0;
  
      HashMap&lt;String, Integer&gt; map = null;
  
      Message m = null;
  
      map = servers.get("Server" + server);
  
      for(String key : map.keySet())
      {
          hostname = key;
          port_no = map.get(key);
      }
      //System.out.println(hostname + " " + port_no);
  
      s = new Socket(hostname, port_no);
      in = new ObjectInputStream(new BufferedInputStream(s.getInputStream()));
      out = new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream()));
  
      s_now = s;
  
      m = new Message(client, data, 0, 0);
      out.writeObject(m);
      out.flush();
      System.out.println("Sent obj");
  </code></pre>
  
  <p>Similarly, the code on the Server side is as follows:</p>
  
  <pre><code>while (true)
      {
          try 
          {
              System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
              Socket server = serverSocket.accept();
              System.out.println("Just connected to " + server.getRemoteSocketAddress());
  
              ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(server.getInputStream()));
              //ObjectOutputStream out = new ObjectOutputStream(server.getOutputStream());
  
              Message m = (Message) in.readObject();
  
              System.out.println(m.value);
          } 
  
          catch (IOException e) 
          {
              e.printStackTrace();
              break;
          }
          catch (ClassNotFoundException e) 
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>The problem is that the object is not getting printed. The output I get is as follows:</p>
  
  <pre><code>Waiting for client on port 1051...
  Just connected to /127.0.0.1:59216
  </code></pre>
  
  <p>Any help in this regard will be greatly appreciated. Thanks :)</p>