title: Java Sockets With Continuous Communication
body: <p>I want to be able to do the following on the client side:</p>
  
  <ul>
  <li>Open socket</li>
  <li>Send Data</li>
  <li>Wait</li>
  <li>Send More Data</li>
  </ul>
  
  <p>And server side:</p>
  
  <ul>
  <li>Open msocket</li>
  <li>Recive data</li>
  <li>Wait</li>
  <li>Keep recivieng (Loop over and over)</li>
  </ul>
  
  <p>Currently i cant make it loop :( </p>
  
  <p>I have client side:</p>
  
  <p>Constctor:</p>
  
  <pre><code>s = new Socket(ServerIPAddr, 6100);
  out = new ObjectOutputStream(s.getOutputStream());
  </code></pre>
  
  <p>Looping function:</p>
  
  <pre><code>    public void sendImage(BufferedImage image){
  
      System.out.println("Starting to send");
      try {
  
          ImageIO.write(image, "PNG", out);
          System.out.println("Sent");
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } // png is lossless
  
  }
  </code></pre>
  
  <p>This can be called many times one after another.</p>
  
  <p>Server side i have consturctor:</p>
  
  <pre><code>    public ServerConnectionThread(Socket clientSocket) {current = clientSocket;}
  </code></pre>
  
  <p>Run function:</p>
  
  <pre><code>    public void run() {
      int i = 0;
      System.out.println("Connection accepted");
      System.out.println("Threaded spanwed");
      try {ObjectInputStream in = new ObjectInputStream(current.getInputStream());
          while (capturing) {
              ImageIO.write(ImageIO.read(in), "PNG", new File("test" + i++
                      + ".png"));
              in.close();
          }
      } catch (IOException e) {e.printStackTrace();}
  
  }
  </code></pre>
  
  <p>Does anyone know why it only works for the first piece of data sent? And refuses to continuosuly sending?</p>
  
  <p>-UPDATE-</p>
  
  <pre><code>while (capturing) {
              BufferedImage b = ImageIO.read(in);
              if(b == null){
                  continue;
              }
              ImageIO.write(b, "PNG", new File("test" + i++
                      + ".png"));
  
          }
  </code></pre>
  
  <p>Seems to fix this issue</p>