开发者

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: Operatio开发者_StackOverflow中文版n 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.


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().


according to the documentation: "you cannot make a silk piurse from a sow's ear"

stream_socket_connect is intended for STREAMS, not datagram packets. recvfrom would be more likely to work in this scenario.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜