0

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 !

Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89
  • It takes a while to get disconnected if a machine suddenly stops; when it does happen, it should signal the socket ready for reading and when you read it there will be zero bytes read. – Ja͢ck Dec 17 '12 at 03:12
  • What do you mean by 'suspend the machine'? @Jack If 'a machine suddenly stops' it either sends a FIN, which is *immediately* seen as zero bytes read, or it sends an RST, which is immediately seen as a 'connection reset by peer', or it sends nothing, which is seen as a read timeout after 'a while'. – user207421 Dec 17 '12 at 09:27
  • "If 'a machine suddenly stops' it either sends a FIN ..." - let me stop you there, doesn't that sound strange to you? :) @EJP – Ja͢ck Dec 17 '12 at 09:44
  • maybe this link: http://stackoverflow.com/questions/9085615/php-detecting-remote-host-disconnection can help you –  Feb 27 '13 at 03:26

2 Answers2

1

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

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.

Florian F
  • 4,044
  • 3
  • 30
  • 31