title: Object In/Out stream. How to send updated, same object repeatedly and efficiently?
body: <p>I've been having some trouble trying to send a list of data using TCP. I've made a smaller version of the problem and exaggerated it a bit.</p>
  
  <p>CLIENT</p>
  
  <pre><code>public class Client {
  
      DListStorage dl = new DListStorage();
      Data d = new Data();
  
      Socket socket;
  
      public Client(){
          d.x = 0;
          dl.dl.add(d);
  
          try {
              socket = new Socket(InetAddress.getLocalHost(), port);
              ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
  
              for(int i = 0; i &lt; 3; i++){
                  oos.writeObject(dl);
                  dl.dl.get(0).x++;
                  System.out.println("Client: " + dl.dl.get(0).x);
              }
  
              socket.close();
              socket = new Socket(InetAddress.getLocalHost(), port);
              oos = new ObjectOutputStream(socket.getOutputStream());
  
              for(int i = 0; i &lt; 3; i++){
                  oos.writeObject(dl);
                  dl.dl.get(0).x++;
                  System.out.println("Client: " + dl.dl.get(0).x);
              }
          } catch (IOException e) {
                  e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>SERVER</p>
  
  <pre><code>public class NewConnections {
  
      ServerSocket ssocket;
      Socket socket;
  
      public NewConnections(){
          acceptConnections.start();
      }
  
      Thread acceptConnections = new Thread(new Runnable(){
          public void run(){
              try {
                  ssocket = new ServerSocket(port, 0, InetAddress.getLocalHost());
                  socket = ssocket.accept();
                  ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
  
                  DListStorage dl;
  
                  for(int i = 0; i &lt; 3; i++){
                      dl = (DListStorage)ois.readObject();
                      System.out.println("Server: " + dl.dl.get(0).x);
                  }
  
                  socket.close();
                  socket = ssocket.accept();
                  ois = new ObjectInputStream(socket.getInputStream());
  
                  for(int i = 0; i &lt; 3; i++){
                      dl = (DListStorage)ois.readObject();
                      System.out.println("Server: " + dl.dl.get(0).x);
                  }
  
                  ssocket.close();
              } catch (IOException | ClassNotFoundException e) {
                      e.printStackTrace();
              }
          }
      });
  }
  </code></pre>
  
  <p>The results for me are as follows.</p>
  
  <pre><code>Client: 0.0
  Client: 1.0
  Server: 0.0
  Client: 2.0
  Server: 0.0
  Server: 0.0
  Client: 3.0
  Client: 4.0
  Server: 3.0
  Client: 5.0
  Server: 3.0
  Server: 3.0
  </code></pre>
  
  <p>Both Data and DataStorage implement Serializable, DataStorage contains
  <code>public List&lt;Data&gt; dl = new ArrayList&lt;&gt;();</code>
  I cant figure out why the server isn't reading the float x properly. Only on the initial read.</p>
  
  <p>Thanks</p>