Re: How to tell if a socket is connected

From: Date: Mon, 12 Jun 2006 23:18:17 +0000
Subject: Re: How to tell if a socket is connected
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-237854@lists.php.net to get a copy of this message
Michael W. wrote:
Hello, Can any of you tell me how to tell whether a socket (from fsockopen()) is connected or not? Specifically, whether the remote server has closed the connection? Stream_get_meta_data() does not do it; in my tests, its output does not change even when the server closes the stream. Thank you, Michael W.
If it is a network socket, and the remote end disconnected unexpectedly (which you MUST assume is a possibility), then the only way to find out if the connection is still open is by SENDING data. Just trying to read it won't cut it. The thing to understand is that when a remote client/server disconnects, it normally safely closes the socket and notifies you. But an abrupt disconnection (Something crashes, loses connectivity, etc) sends no such thing. The only way to find out that a connection is really lost is to write some data and see if it times out or not. Regards, Adam Zey.

« previous php.general (#237854) next »