The hosting service that I use currently does not let me use sockets, probably for good reason on their part. They do, however, let me use fsockopen. I was wondering what the difference is, because some scripts that worked with socket_create and even stream_socket_server, do not work with fsockopen. That said, if fsockopen should work, my code is listed below. What it does is it listens on its own ip address for incoming udp packets and reads them.
Thanks
$sock = fsockopen("udp://x.x.x.x", $port);
while(1)
{
$buf = fread($sock, 200);
flush();
ob_flush();
}
The fsockopen() function opens an Internet or Unix domain socket connection.
The function stream_socket_client() is similar but provides a richer set of options, including non-blocking connection and the ability to provide a stream context.
This sample code checks to see if the fsockopen() function is available. To check for another function, change the $function_name variable's value: <? php $function_name = "fsockopen"; if ( function_exists($function_name ) ) { echo "$function_name is enabled"; } else { echo "$function_name is not enabled"; } ?>
fsockopen creates a connection to a host, not a listening socket.
fsockopen($address) ~== socket_connect(socket_create(), $address)
Your hosting provider doesn't want you listening on alternate ports/protocols.
If what you have works, I wouldn't count on it always working as it would be a bug.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With