Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket_create vs. fsockopen php

Tags:

php

sockets

udp

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();
}
like image 483
Samuel Avatar asked Feb 18 '09 22:02

Samuel


People also ask

What is Fsockopen PHP?

The fsockopen() function opens an Internet or Unix domain socket connection.

Is similar to Fsockopen () in PHP?

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.

How do I check Fsockopen?

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"; } ?>


1 Answers

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.

like image 189
Ryan Graham Avatar answered Sep 19 '22 13:09

Ryan Graham