Swoole TCP&UDP客户端
1.创建客户端数据 client.php
<?php //连接Swoole tcp服务 $client = new Swoole\Client(SWOOLE_SOCK_TCP); if (!$client->connect('127.0.0.1', 1111, 0.5)) { echo "connect failed. Error: {$client->errCode}\n"; exit; } //php cli常量 fwrite(STDOUT,"请输入消息"); $msg = trim(fgets(STDOUT)); //发送消息给tcp server服务器 $client->send($msg); //接受来自server返回的数据 echo $client->recv(); $client->close();
测试服务端返回数据
Client: fd:14-reactor_id:0-Connect.
测式客户端返回数据
请输入消息3333
Server: reactor_id:0-fd:14-data:333
1.创建客户端数据client.php
<?php //连接Swoole tcp服务 $client = new Swoole\Client(SWOOLE_SOCK_UDP); if (!$client->connect('127.0.0.1', 2222, 0.5)) { echo "connect failed. Error: {$client->errCode}\n"; exit; } //php cli常量 fwrite(STDOUT,"请输入消息"); $msg = trim(fgets(STDOUT)); //发送消息给tcp server服务器 $client->send($msg); //接受来自server返回的数据 echo $client->recv(); $client->close();
测试客户端返回数据
请输入消息333
Server:333
测试服务器返回数据
array(4) {
["server_socket"]=>
int(3)
["server_port"]=>
int(2222)
["address"]=>
string(9) "127.0.0.1"
["port"]=>
int(46175)
}