Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating s simple client/server UDP example in PHP

I'm trying to make a simple UDP client server example in PHP but I face an error.

This is the client :

$fp = stream_socket_client("udp://192.168.0.12:12478", $errno, $errstr);

if ($fp)
{
        fwrite($fp, "TEST 1 TEST 2 TEST 3");
        $buf = fgets($fp);
        var_dump($buf);
        fclose($fp);
}

This is the server :

$socket = stream_socket_server("udp://192.168.0.12:12478", $errno, $errstr, STREAM_SERVER_BIND);
if ($socket)
{
  while ($conn = stream_socket_accept($socket)) {
    fwrite($conn, date("D M j H:i:s Y\r\n"));
    fclose($conn);
  }
  fclose($socket);
}

All executions end with :

Warning: stream_socket_accept(): accept failed: Operation not supported 

Basically, this is the example given in all PHP documentations but I can't figure what is wrong in it. Any help is greatly appreciated.

Thanks.

like image 413
Litre Avatar asked Oct 19 '25 00:10

Litre


1 Answers

Here is the warning on the very same page

Warning
This function should not be used with UDP server sockets. Instead, use stream_socket_recvfrom() and stream_socket_sendto().

like image 127
Srisa Avatar answered Oct 21 '25 15:10

Srisa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!