title: Java(Kotlin) Socket Server. Read continously
body: <p>My server reading looks like this:</p>
  
  <pre><code>private fun init() {
          val inStream = BufferedInputStream(socket.getInputStream())
          val bytes = ByteArray(bufferSize)
  
          outStream = DataOutputStream(socket.getOutputStream())
  
  
          while (true) {
              val count = inStream.read(bytes, 0, bufferSize)
  
              if (count &gt;= 0) {
                  server.onReceive(this, bytes, count)
              }
  
          }
      }
  </code></pre>
  
  <p>First read waits until bytes received. But second doesn't wait. And loop is running forever and count is -1. I want to read bytes only when they received.</p>