Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if socket is open in PHP?

Tags:

php

sockets

I am required to maintain a few sockets that I opened with PHP and check those sockets at regular intervals. I am new to sockets in PHP; I opened the sockets like this:

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

Some of the sockets can get into a state where they don't return messages; these sockets only receive messages. How can I tell if a socket is opened if the socket does not respond to a message?

like image 929
John R Avatar asked May 14 '12 20:05

John R


1 Answers

socket_sendto — Sends a message to a socket, whether it is connected or not

Seems like you could use socket_sendto and send some 'ping' data to the remote host, then test the return value to determine if the socket is still established.

like image 87
Mike Purcell Avatar answered Oct 19 '22 22:10

Mike Purcell