php网络编程swoole udp

服务端:

<?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();

 

输出:

 

posted @ 2024-12-23 20:38  河北大学-徐小波  阅读(6)  评论(0编辑  收藏  举报