LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-20-2007, 02:20 AM   #1
baddah
Member
 
Registered: Feb 2006
Location: Cape Town,South Africa
Distribution: Fedora Core 8
Posts: 183

Rep: Reputation: 30
PHP - Check if Socket is alive


Hi,

I've got a PHP Socket client that connects to a server.If the connect is succesfull,it reads from it.My problem is that,if the server goes down,i must detect that the server is down and try to reconnect.

What function(method) can i use to check if a socket is still alive.The socket was made with socket_create,and socket_connect.

Thanks for any help.

Code:
while (1) {

        usleep(500000);         //sleep for 150 ms

        $socket = socket_create(AF_INET, SOCK_STREAM, 0);
        $sk = socket_connect($socket,$host,$port);

        socket_set_nonblock($socket);

        if (!$sk) {
                echo "Not connected Yet \n";
        }
        else {
                while (1) {

                        usleep(500000);

                        //check for incoming messages from server
                        $result = socket_read($socket,2048);

                        if ($result != "") {
                                echo "process Data";
                        }

                        //check if there is something to send
                        $sql = "select * from queuebuffers";
                        echo "$sql";
                        $res=mysqli_query($hoteldb,$sql);
                        if (@mysqli_num_rows($res) > 0) {
                                while ($row=mysqli_fetch_object($res)) {
                                        //send data to server
                                }
                        }

                /*
                In here i want to check if the socket is still connected,if not i want to go out this loop and
                into the first loop again to try an reconnect in
                */

                }
        }
}
 
Old 11-20-2007, 04:47 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,115

Rep: Reputation: 218Reputation: 218Reputation: 218
Hi

socket_read will return false if there was an error, and an empty string if there was no data. So after the line, you should check the return value.

PHP Code:
$result socket_read($socket,2048);
if (
$result === false) {
  
socket_shutdown($socket2);
  
socket_close($socket);
  break;

Edit: Added socket_shutdown and socket_close - I think it will be safer.

Last edited by Guttorm; 11-20-2007 at 04:51 AM.
 
Old 11-20-2007, 06:44 AM   #3
baddah
Member
 
Registered: Feb 2006
Location: Cape Town,South Africa
Distribution: Fedora Core 8
Posts: 183

Original Poster
Rep: Reputation: 30
Hi,

Thanks for the reply.Does not look like the read returns no data when there is nothing to read.

The ($result === false) is true in cases when there is nothing to read as well.I'll play around a bit,maybe i can find it.

Thanks
 
Old 11-20-2007, 07:00 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,115

Rep: Reputation: 218Reputation: 218Reputation: 218
Hmm. I thought it would return an empty string. Maybe also check with the socket_last_error function?

Edit:
If it does return false when there is no data, there is a bug in PHP. At least it contradicts the manual:

Note: socket_read() returns a zero length string ("") when there is no more data to read.

(From http://www.php.net/manual/en/function.socket-read.php)

Note the difference between == and ===. Are you sure you test with ===?

("" == false) is true.
("" === false) is false.

Last edited by Guttorm; 11-20-2007 at 07:12 AM.
 
Old 11-20-2007, 07:22 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by Guttorm View Post
Hi

socket_read will return false if there was an error, and an empty string if there was no data. So after the line, you should check the return value.

PHP Code:
$result socket_read($socket,2048);
if (
$result === false) {
  
socket_shutdown($socket2);
  
socket_close($socket);
  break;

Edit: Added socket_shutdown and socket_close - I think it will be safer.
you can try
Code:
...
if ( strlen($result) < 1 )
{
   ...
}
....
 
Old 12-11-2007, 03:02 AM   #6
baddah
Member
 
Registered: Feb 2006
Location: Cape Town,South Africa
Distribution: Fedora Core 8
Posts: 183

Original Poster
Rep: Reputation: 30
Hi,

I'm still having problems with detecting when my host closes the connection.I have tried various things,looking at what socket_read returns and working with socket_last_error.

Here's a small client script

Code:
<?

$sock = socket_create(AF_INET, SOCK_STREAM, 0);
$sk = socket_connect($sock,"192.168.101.1","1234");
socket_set_nonblock($sock);

while (1) {

        sleep(2);
        $buffer=socket_read($sock,512);
        echo "Buffer = $buffer \n";
        echo "Last Error = ".socket_last_error($sock).socket_strerror(socket_last_error($sock))."\n";

}

?>
I get the same outpur from buffer and socket_last_error if i close the connection what i get when the buffer is empty!Is this a bug in php or am i doing something wrong?I really hope i'm doing something wrong.

Here's the output i get.

Code:
[root@localhost sockets]# php test.php
Buffer = LS|DA071123|TI113905|                    //reading data in buffer
Last Error = 0Success
Buffer =
Last Error = 11Resource temporarily unavailable   //nothing to read (buffer emtpy)
Buffer =
Last Error = 11Resource temporarily unavailable
Buffer =
Last Error = 11Resource temporarily unavailable   //at this point i closed the host,but buffer and error stays the same!
Buffer =
Last Error = 11Resource temporarily unavailable
Buffer =
Last Error = 11Resource temporarily unavailable
Is there any other way i can detect this,i really need to be able to do this.I have tried using PHP_NORMAL_READ as well in my socket_read.That does not work as well.

Any suggestion would really be appreciated.Thanks

Last edited by baddah; 12-11-2007 at 03:04 AM. Reason: Incomplete
 
Old 12-11-2007, 08:50 AM   #7
baddah
Member
 
Registered: Feb 2006
Location: Cape Town,South Africa
Distribution: Fedora Core 8
Posts: 183

Original Poster
Rep: Reputation: 30
Ok,This is very weird

If the socket is connected and the buffer is empty the following returns false...

Code:
$result = socket_read($socket,2000);

if ($result === "") {
      //it goes in here when the socket (host) has disconnected
}
else {
     // it goes in here when the buffer is empty or the buffer has data in it
}
So it works now,i can detect when the host closes the socket,but it seems to not be as the online documentation of PHP describes.

Thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to check if a socket is valid in C ? naihe2010 Programming 3 10-29-2006 06:56 PM
How to check the child process alive ? stny Programming 2 10-16-2006 05:12 PM
Socket doesn't time out. (PHP) laserbeamninja Programming 0 04-18-2005 03:23 PM
mysql - php socket error. ldp Linux - Software 9 10-22-2004 03:40 AM
check if server is alive by ping saavik Linux - Networking 1 05-06-2002 09:36 AM


All times are GMT -5. The time now is 09:20 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration