Can a PHP script determine if a socket is closed?

How does a PHP script detect that a socket has been closed by the remote side?

+4
source share
2 answers

From socket_read () :

  socket_read () returns the data as a string on success, or FALSE on error 
 (including if the remote host has closed the connection).  The error code can 
 be retrieved with socket_last_error ().  This code may be passed to 
 socket_strerror () to get a textual representation of the error.

This is a pretty standard detection approach if the socket is closed in most languages. I do not believe that PHP offers a direct notification of the style of the event (with the possible exception of something in PEAR).

+4
source

fread($socket) returns an empty string instantly, without waiting for a socket timeout.

(No, this is not a real answer, but it is a hacking solution that I have in my ATM code. This behavior may also be specific to Windows.)

0
source

All Articles