Re: How to tell if a socket is connected
| From: | Michael W. | Date: | Tue, 13 Jun 2006 02:09:19 +0000 |
| Subject: | Re: How to tell if a socket is connected | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-237863@lists.php.net to get a copy of this message | ||
Adam Zey wrote:
> 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.
How can I tell whether the write timed out? Does fwrite() return false, or
do I have to check for the 'timed_out' flag in the return of
stream_get_meta_data()?
Thanks a lot,
Michael W.