Laurent Crivello
Laurent Crivello

Reputation: 3931

Remote socket disconnection not seen by PHP

I have a PHP socket server listening to multiple sockets.
On my client, if I suspend the machine, no disconnection message is sent to the PHP server. How can I figure out which client is still connected or not, or get these disconnection events ?
In PHP, I use socket_select to listen to all my sockets.

Thanks !

Upvotes: 0

Views: 337

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409442

If the remote socket is properly closed, then your socket will become readable but the receive call will return no data. If there is an error in the communication, the next receive or send operation will return an error.

However if the connection just goes idle, then you will not know if the other end is alive or not. You can set a socket option (SO_KEEPALIVE) to automatically have the system send "keep-alive" messages and discover if there is any problems. The problem with SO_KEEPALIVE is that the default timing for this is two hours. If you need shorter times (you most likely do) then your best bet is to have your own keep-alive messaging implemented as part of the protocol.

Upvotes: 1

Florian F
Florian F

Reputation: 4363

socket_read will return false if there was an error, and an empty string if there was no data. These things dont trigger events, you have to test if the connection is still alive.

Upvotes: 0

Related Questions