PHP Socket Client - Server Shutdown Detection

I have a socket client that connects to the server and waits for lines of text to be sent from the server.

I open the connection with:

$handle = fsockopen(MY_IP_ADDRESS,MY_PORT,$sockErrno,$sockErrStr);
stream_set_blocking($handle,1);
stream_set_timeout($handle,MY_SOCKET_TIMEOUT);

Then:

while (true) {
   $inString = fgets($handle,256);
   do some stuff with the received data... 
}

All of this works great. There is no specific interval between received messages. Messages may arrive several times per second or not within a few minutes.

The stream stream_set_timeout is set to 60 seconds ... if the message has not been received, I just loop and reload the frames.

From time to time, the connection is broken for one reason or another.

I want to be able to detect a broken connection so that I can start trying to reconnect.

? -, , - , , ?

+1

Source: https://habr.com/ru/post/1654124/


All Articles