title: Transfer an image as byte array
body: <p>I'm trying to send an image from a 'server', whose purpose is to send images, to a 'client' that is an Android device. The socket between then is stay open and images keep on coming. The problem is:</p>
  
  <ol>
  <li>I dont know the size of the image (it can change);</li>
  <li>the image is not BMP format.</li>
  </ol>
  
  <p>In my code below:</p>
  
  <ol>
  <li>the client doesn't stop reading after the image is transfered (I don't get <code>-1</code> when it is read), it will stop just when the socket is closed;</li>
  <li>after it has finished reading, the input stream is not cleared and the same image is read over and over again.</li>
  </ol>
  
  <p>At first I tried to send 2 BMP images whose size I know (both the same size). Here is the server code:</p>
  
  <pre><code>    public static void main(String[] args) {
          try {
              ServerSocket s = new ServerSocket(4488);
              System.out.println("Waiting for a connection . . . . ");
              Socket socket = s.accept();
              System.out.println("Connected!");
              DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
              Thread.sleep(2000);
  
              File myImageFile = new File("c:\\1\\a.bmp");
              FileInputStream fis = new FileInputStream(myImageFile);
              byte[] data = new byte[(int) myImageFile.length()];
              byte[] tmp = new byte[0];
              byte[] myArrayImage = new byte[0];
              int len = 0 ;
              int total = 0;
  
              while( (len = fis.read(data)) != -1 ) {
                  total += len;
                  tmp = myArrayImage;
                  myArrayImage = new byte[total];
                  System.arraycopy(tmp,0,myArrayImage,0,tmp.length);
                  System.arraycopy(data,0,myArrayImage,tmp.length,len);
              }
  
              //
  
              fis.close();
  
              dataOutputStream.write(myArrayImage);
              dataOutputStream.flush();
              Thread.sleep(2000);
  
              myImageFile = new File("c:\\1\\b.bmp");
              fis = new FileInputStream(myImageFile);
              data = new byte[(int) myImageFile.length()];
              tmp = new byte[0];
              myArrayImage = new byte[0];
              len = 0 ;
              total = 0;
  
              while( (len = fis.read(data)) != -1 ) {
                  total += len;
                  tmp = myArrayImage;
                  myArrayImage = new byte[total];
                  System.arraycopy(tmp,0,myArrayImage,0,tmp.length);
                  System.arraycopy(data,0,myArrayImage,tmp.length,len);
              }
  
              fis.close();
  
              dataOutputStream.write(myArrayImage);
              dataOutputStream.flush();
              Thread.sleep(2000);
  
              socket.close();
              s.close();
  </code></pre>
  
  <p>Here is the client code:</p>
  
  <pre><code>    @Override
  public void run() {
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      byte[] byteChunk = null;
      int c;
  
      if (_dataInputStream != null) {
          while (true) {
              try {
                  Thread.sleep(200);
              } catch (Exception e) {
              }
  
              try {
                  byteChunk = new byte[1024];
  
                  while ((c = _dataInputStream.read(byteChunk)) != -1){
                      buffer.write(byteChunk, 0, byteChunk.length);
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
  
  
              final Bitmap bitmap = BitmapFactory.decodeByteArray(buffer.toByteArray(), 0, buffer.size());
  
              if (bitmap != null) {
                  _handler.post(new Runnable() {
                      public void run() {
                          _iv.setImageBitmap(bitmap);
                          _iv.invalidate();
                      }
                  }); 
              }
  
                  buffer.reset();
          }
      }
  }
  </code></pre>
  
  <p>Now, as I said, the client is stuck on the while loop until the socket is closed and then (no matter how many images I send, because I created in the client side a bytearray that fits only for 1 image) it keeps reading this first image all the time.</p>
  
  <p>Any ideas what I am doing wrong?</p>
  
  <p>P.s. If the image is not a BMP will the decode I wrote in the client side code work? The real image I want to send is a PNG.</p>