1.1.1. 快速开始 UDP

server_udp.php

<?php
//创建Server对象,监听 127.0.0.1:5200端口,类型为SWOOLE_SOCK_UDP
$server = new swoole_server('127.0,0,1', '5200', SWOOLE_PROCESS, SWOOLE_SOCK_UDP);

//监听数据接收事件
$server->on('Packet', function ($server, $data, $clientInfo) {
    $server->sendto($clientInfo['address'], $clientInfo['port'], "Server " . $data);
    var_dump($clientInfo);
});
// 开始监听
$server->start();

udp 服务不需要连接,直接监听 使用下面命令进行测试

netcat -u 127.0.0.1 5200

结果: 服务器端:

vagrant@homestead:~/Code$ php server_udp.php
array(4) {
  ["server_socket"]=>
  int(3)
  ["server_port"]=>
  int(5200)
  ["address"]=>
  string(9) "127.0.0.1"
  ["port"]=>
  int(32951)
}

客户端:

vagrant@homestead:~/Code$ netcat -u 127.0.0.1 5200
11
Server 11
Copyright © Kagami丶 2019 all right reserved,powered by Gitbook该文件修订时间: 2019-12-10 16:42:15

results matching ""

    No results matching ""