swoole TCP协议同步堵塞通信

php 服务端 s.php

//创建Server对象,监听 0.0.0.0:9501端口
$serv = new Swoole\Server("0.0.0.0", 9501); 

//监听连接进入事件
$serv->on('Connect', function ($serv, $fd) {  
    echo "Client: Connect.\n";
});

//监听数据接收事件
$serv->on('Receive', function ($serv, $fd, $from_id, $data) {
    $serv->send($fd, "Server: ".$data);
});

//监听连接关闭事件
$serv->on('Close', function ($serv, $fd) {
    echo "Client: Close.\n";
});

//启动服务器
$serv->start(); 

 

php 客户端 c.php

$client = new Swoole\Client(SWOOLE_SOCK_TCP);
if (!$client->connect('127.0.0.1', 9501, -1)) {
    exit("connect failed. Error: {$client->errCode}\n");
}
$client->send("hello world\n");
echo $client->recv();
$client->close();

 

 

执行:

©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页