PHP swoole UDP服务端和客户端
- 服务端
<?php $serv = new swoole_server('127.0.0.1',9501,SWOOLE_PROCESS,SWOOLE_SOCK_UDP); $serv->on('Packet',function ($serv,$data,$clientinfo) { $serv->sendto($clientinfo['address'],$clientinfo['port'],"发来的数据{$data}\n"); echo $data."\n"; }); $serv->start(); ?>
- 客户端
<?php $client = new swoole_client(SWOOLE_SOCK_UDP); if(!$client->connect('127.0.0.1',9501)) { echo "I'm deeply sorry"; } fwrite(STDOUT,"说点儿什么吧\n"); $msg = trim(fgets(STDIN)); $client->send($msg); $ret = $client->recv(); echo $ret; ?>