server.php
<?php $server = new swoole_server(‘127.0.0.1‘, 9502, SWOOLE_PROCESS, SWOOLE_SOCK_UDP); $server->set(array("worker_num"=>4, "dispatch_mode"=>2)); $server->on("connect", function($cli) { $ser->send("hello"); }); $server->on("receive", function($ser, $fd, $from_id, $data) { $info = $ser->connection_info($fd, $from_id); var_dump($info); $ser->send($fd, "hello", $from_id); }); $server->start();
client.php
<?php $client = new swoole_client(SWOOLE_SOCK_UDP); $client ->connect("127.0.0.1", 9502, 0.5); $client->send("dddddddddd444444444d"); $data=$client->recv(); echo $data;
查看通信过程
[[email protected] ~]# tcpdump -i any udp port 9502 tcpdump: WARNING: Promiscuous mode not supported on the "any" device tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on any, link-type LINUX_SLL (Linux cooked), capture size 96 bytes 08:30:18.545684 IP localhost.localdomain.38978 > localhost.localdomain.9502: UDP, length 20 08:30:18.549676 IP localhost.localdomain.9502 > localhost.localdomain.38978: UDP, length 5
时间: 2024-10-09 12:03:15