title: Java: Very Slow Communication with sockets when on LAN
body: <p>I have a  class in Java that needs to run on a separate machine due to RAM requirements. The class requires ~5GB or RAM. Thus, I have developed a solution that uses sockets in order to allow the machines to communicate.
  For simplicity lets assume that the heavy class under a socket server wrapper is the Service Class. The client class that connects to the socket server lets assume is the Client Class.</p>
  
  <p>When the Socket Server runs locally everything is perfect. The server runs as a standalone app and there is no need to reload the ~5GB every time I start my experiments. However, I realized that when the Service Class runs on a different machine over LAN the performance is terribly slow. The issue is not the transfer rate since when monitoring it I see ~50K/s max transfer rate.</p>
  
  <p>On the client class that connects to the Server uses Buffering using <code>BufferedInput</code>/<code>Output</code> stream.</p>
  
  <p>Let me provide the <strong>Server Thread:</strong></p>
  
  <pre><code>public void run() {
          try
          {
              ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
  
              ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
              ServerTuple tuple;
              double i=0;
              while ((tuple = (ServerTuple) ois.readObject()) != null) {
                  double[] vector;
                  String action=tuple.action;
                  String content=tuple.content;
                  if(action.equals("wordVector"))
                  {
                      vector=Word2Vec.getWordVector(tuple.content);
                  }
                  else if(action.equals("avgVector2"))
                  {
                      content=content.replaceAll("\\s+", " ");
                      vector=Word2Vec.getWordVector2(content.split(" "));
                  }
                  else
                      vector=null;
                tuple.setVector(vector);
                oos.writeObject(tuple);
                oos.flush();
              }
              //oos.close();
  
          }
          catch(Exception e){
              return;
          }
  
  
      }
  </code></pre>
  
  <p>And the <strong>Client Procedure:</strong></p>
  
  <pre><code>public static void init(String host,int port) throws UnknownHostException, IOException {
          socket=new Socket(host,port);
          ois = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
          oos = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
      }
  
  public static ServerTuple sendData(ServerTuple tuple) throws UnknownHostException, IOException
      {
          try
          {
              if(socket==null)
              {
                  init();
              }
              oos.writeObject(tuple);
              oos.flush();
              ServerTuple returnTuple;
              while ((returnTuple = (ServerTuple) ois.readObject()) != null) {
                  if(returnTuple.vector==null)
                      return null;
                return returnTuple;
              }
              return null;
          }
          catch(Exception e)
          {
              e.printStackTrace();
              return null;
          }
      }
  </code></pre>