Re: Socket trouble
| From: | bbonkosk at tampabay dot rr dot com | Date: | Thu, 28 Oct 2004 17:21:22 +0000 |
| Subject: | Re: Socket trouble | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-200563@lists.php.net to get a copy of this message | ||
To prevent the blocking and your CPU going up to 100% usages look into:
http://us2.php.net/manual/en/function.socket-select.php
For length of a string:
http://us2.php.net/manual/en/function.strlen.php
----- Original Message -----
From: René Fournier <m5@renefournier.com>
Date: Thursday, October 28, 2004 1:04 pm
Subject: [PHP] Socket trouble
> I'm having some trouble with my socket client. I would like it to
> "notice" when the server is no longer there, and then reconnect. I
> have
> code for the reconnect, etc., but I can't figure out how to write
> the
> socket_read code so that it "notices" when the server is gone and
> then
> responds. Presently, my client just hangs (CPU goes to 100%,
> nothing
> happens.)
>
> Here's my code:
>
> while(($buf = socket_read($socket,1,PHP_BINARY_READ)) !== false) {
> $data .= $buf;
> if(preg_match("/TERMINATING/",$data)) {
> $msg_recv = 1;
> break;
> }
> }
>
> The PHP docs on socket_read suggest how this might be done:
>
> "Note: socket_read() may return a zero length string ("")
> indicating
> the end of communication (i.e. the remote end point has closed the
> connection)."
>
> Does this "end of communication" include cases when the socket
> server
> dies without gracefully disconnecting? How could my code check for
> this
> zero length string? Any help is much appreciated. Thanks!
>
> ...René
>
> ---
> René Fournier
> www.renefournier.com
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>