title: Java socket read inputstream without blocking
body: <p>I have a little problem with implementing socket connection between Android and IOS. While I'm using my application to connect two Android running devices everything works fine. But when I have to receive some data from Iphone app my readStream function is blocking and I can receive all data after the other part close the socket and it that way I can't return any response back to it. Here is what I am using for listening :</p>
  
  <pre><code>try {
      serverSocket = new ServerSocket(5000);
      Log.d("","CREATE SERVER SOCKET");
  } catch (IOException e) {
       e.printStackTrace();
  }
  
  while(state){
      try {
          if(serverSocket!=null){
              client = serverSocket.accept();
              client.setKeepAlive(true);
              client.setSoTimeout(10000);
  
              // LOGS
              Log.w("READ","is connected : "+client.isConnected());
              Log.w("READ","port : "+client.getPort());
              Log.w("READ","ipadress : "+client.getInetAddress().toString());
  
  
  
              InputStream is = client.getInputStream();
  
              Log.w("READ","is Size : "+is.available());
  
              byte[] bytes = DNSUtils.readBytes(is);
              Log.v("","-------------------------");
              for(int i=0;i&lt;bytes.length;i++){
                  Log.w("READ","bytes["+i+"] : "+bytes[i]);
              }
              Log.v("","-------------------------");
  
              try {
                  Log.w("READ","packetType : "+bytes[4]);
  
                  if(bytes!=null)
                      DNSUtils.getPacketType(bytes[4], bytes, client);
  
              } catch(Exception e){
                  e.printStackTrace();
              }
          }
      } catch (IOException e) {
          e.printStackTrace();
          Log.v("","IOException");
          ResponseERR pack = new ResponseERR();
          ResponseERR.errorMsg = "Socket TimeOut exception!";
          byte[] packet = pack.createNewPacket();
  
          try {
              if(client!=null){
                  OutputStream out = client.getOutputStream();
                  out.write(packet);
                  out.flush();
                  client.shutdownOutput();
                  client.close();
                  Log.e("READDATAFROMSOCKET","ResponseERR Send.");
              }
          } catch (Exception e1) {
              e1.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>and here is the function which I am using to convert <code>InputStream</code> to <code>Byte Array</code> : </p>
  
  <pre><code>public static byte[] readBytes(InputStream inputStream) throws IOException {
      // this dynamically extends to take the bytes you read
      ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
  
      // this is storage overwritten on each iteration with bytes
      int bufferSize = 1024;
      byte[] buffer = new byte[bufferSize];
  
      // we need to know how may bytes were read to write them to the byteBuffer
      int len = 0;
      while ((len = inputStream.read(buffer)) != -1) {
          byteBuffer.write(buffer, 0, len);
      }
  
      // and then we can return your byte array.
      return byteBuffer.toByteArray();
  }
  </code></pre>
  
  <p>The both Iphone / Android app works like this:</p>
  
  <ul>
  <li>First it creates Socket and send data to other device and close the OutputStream. </li>
  <li>The second part is parsing the byte array and then return response using the same socket.</li>
  </ul>
  
  <p>Any ideas how to change my functions so I can read the inputstream without blocking?</p>
  
  <h2>EDIT:</h2>
  
  <blockquote>
    <p>So the problem was on IOS side, it seems that Apple API require to
    close both streams of the created socket : read/write so the byte
    array can be send, which seems really stupid, because in that way the
    iphone app can't receive and parse my response.</p>
  </blockquote>