How to find out if a socket is open in PHP?

I need to support several sockets that I opened with PHP, and check them regularly. I am new to sockets in PHP; I opened sockets as follows:

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_connect($socket, $ip, $port); 

Some sockets may fall into a state in which they do not return messages; these sockets only accept messages. How do I know if a socket is open if the socket does not respond to the message?

+8
php sockets
source share
2 answers

socket_sendto - Sends a message to the socket, regardless of whether it is connected or not

It looks like you can use socket_sendto and send some ping data to the remote host, then check the return value to determine if the socket is still set.

+2
source share

Better use function: fsockopen ()

Read this: http://www.php.net/manual/en/function.fsockopen.php

or you can try using the function: socket_create_listen ()

Read this: http://www.php.net/manual/en/function.socket-create-listen.php

(this one is only experimental.)

and check it regularly using a simple cron.

0
source share

All Articles