服务端:
<?php $server = new Swoole\Server('127.0.0.1', 9504, SWOOLE_PROCESS, SWOOLE_SOCK_UDP); $server->on('start', function ($server) { echo "UDP Server is started at udp://127.0.0.1:9504\n"; }); $server->on('packet', function ($server, $data, $clientInfo) { echo "接收到客户端数据:{$data},来自{$clientInfo['address']}:{$clientInfo['port']}\n"; $server->sendTo($clientInfo['address'], $clientInfo['port'], "我收到你的消息了"); }); $server->start();
客户端:
<?php $client = new Swoole\Client(SWOOLE_SOCK_UDP); $client->sendto('127.0.0.1', 9504, "测试一下"); echo "收到服务端回复:".$client->recv(); //var_dump($client->isConnected()); //$socketinfo = $client->getsockname(); //print_r($socketinfo); //$peerinfo = $client->getpeername(); //print_r($peerinfo); $client->close();
输出:
标签:echo,udp,clientInfo,swoole,0.1,server,client,php From: https://www.cnblogs.com/xuxiaobo/p/18624990