0

I get an InputStream from a TCP connection and read from it (via BufferedReader). When receiving a message, my program will process that request. This includes some database operations. It will do so continuously in a while loop (TCP long connection).

Schematically it looks like this:

while(!clientSocket.isClosed()) {
    in.read(request);
    // process request
}

What happens, if a new message arrives, while my program is still busy processing the previous request? Will it be "stored" and fetched with the next in.read() call? Or do I have to create a thread for the database operations to not miss requests?

tweekz
  • 187
  • 1
  • 9
  • 1
    Indeed the data received via Socket is stored. However, nothing prevents you from dictating a thread for this, most of the time it's a good idea. Nevertheless, your code will probably never complete successful. This's because `isClosed` only return true if the server ITSELF closes the connection (via [close()](https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#close())), not the client. This issue is discussed [here](https://stackoverflow.com/a/10241044/14790684). – Al-Anazi Dec 16 '22 at 20:52
  • Thank you very much. You could post this as answer, so I can check it. – tweekz Dec 16 '22 at 21:24

0 Answers0