title: How to pass object by socket in java Server Client
body: <p>I'm new in Java Sockets, I have seen so many examples but I can't understand how to pass an argument from server to client and vice versa. My destination is to pass an Object that's why I'm using Object I/O Stream.</p>
  
  <p>I have to classes Server and Player.</p>
  
  <pre><code>public class Server extends Thread{
  public static final int TEST = 165;
  
  ServerSocket serverSocket;
  InetAddress address;
  
  Player playerWhite;
  
  public Server() {               
      start();
  }
  
  @Override
  public void run() {
      try
      {
          address = InetAddress.getLocalHost();
          serverSocket = new ServerSocket(6000);
          playerWhite = new Player();
  
          System.out.println("server waits for players");
          playerWhite.socket = serverSocket.accept();
          playerWhite.start();
          sendTestMessage(playerWhite);
  
      } 
      catch (IOException ex) 
      {
          Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
      }               
  }
  
  public void sendTestMessage(Player player) throws IOException
  {
      ObjectOutputStream testToClient = new ObjectOutputStream(player.socket.getOutputStream());        
      testToClient.write(TEST); 
      testToClient.flush();        
  }
  </code></pre>
  
  <p>And the Player class:</p>
  
  <pre><code>public class Player extends Thread {   
  Socket socket;
  
  Player() throws IOException
  {
      socket = new Socket(InetAddress.getLocalHost(), 6000);
  }
  
  @Override
  public void run() {
      try {            
          listenTestStream();
      } 
      catch (IOException | ClassNotFoundException ex) 
      {
       Logger.getLogger(CheckerPlayer.class.getName()).log(Level.SEVERE, null, ex);
      }
  }
  
  public void listenTestStream() throws IOException, ClassNotFoundException
  {
      ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
      int message = ois.readInt();
  
      //To test
      System.out.println("Server listened: " + message);
  }
  </code></pre>
  
  <p>I execute it as create a Server object in the other class.</p>
  
  <p>When I have testing this application I saw that sometimes client is faster than Server. Is it possible to make him "wait" for server response?
  Thanks for your response.</p>
  
  <p><strong>EDIT 1: PROBLEM SOLUTION:</strong></p>
  
  <p>From outside we should create:</p>
  
  <p><code>Player player = new Player(); // (class player extends from Thread)
  player.start();</code></p>
  
  <p>and delete the Player variable - is not necessary, we need only Socket so:
  Server: </p>
  
  <p><code>Socket playerWhiteSocket</code></p>
  
  <pre><code>public void run() {
  try
  {
      serverSocket = new ServerSocket(PORT);
      playerWhiteSocket = serverSocket.accept();
      sendMessage(playerWhiteSocket, "Hello");
  } 
  catch(IOException | ClassNotFoundException ex)
  {}
  
  public void sendMessage(Socket socket, String message) throws IOException
  {
      ObjectOutputStream testToClient = new ObjectOutputStream(socket.getOutputStream());        
      testToClient.writeObject(message);
      testToClient.flush();        
  }
  </code></pre>
  
  <p>In Player class we need get method:</p>
  
  <pre><code>public String receiveMessage() throws IOException, ClassNotFoundException
  {
      //socket is a variable get from Player class socket = new Socket("severHost", PORT);
      ObjectInputStream messageFromServer = new ObjectInputStream(socket.getInputStream()); 
      String message = (String) messageFromServer.readObject();
      return message;
  }   
  </code></pre>