Skip to content
Advertisement

Detect client is disconnected

I have a simple program that listens to a socket. Everything goes fine except when the connection is lost in while(1) cycle. In this case the program falls into read from socket for many times without result. How can I detect a disconnected client in while(1) cycle?

JavaScript

Advertisement

Answer

Change:

JavaScript

To:

JavaScript

Also, your code mishandles the case where the first byte is zero. And you can’t avoid this case because TCP does not preserve application message boundaries. You should just pass br to printHex so it will know how many characters to print.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement