23 lines
526 B
PHP
23 lines
526 B
PHP
|
<?php
|
||
|
// 建立client对象并指定服务器类型为UDP
|
||
|
$client = new Swoole\Client(SWOOLE_SOCK_UDP);
|
||
|
|
||
|
// 连接服务器,失败则停止
|
||
|
if (!$client->connect('127.0.0.1', 9502)) {
|
||
|
echo 'Connection fail!';
|
||
|
exit();
|
||
|
}
|
||
|
|
||
|
// 接收控制台输入的消息
|
||
|
fwrite(STDOUT, 'Please send your name: ');
|
||
|
$message = trim(fgets(STDIN));
|
||
|
|
||
|
// 发送消息给TCP服务器
|
||
|
if (!$client->send($message)) {
|
||
|
echo 'Send fail!';
|
||
|
exit();
|
||
|
}
|
||
|
|
||
|
// 接收TCP服务器返回的数据并显示
|
||
|
$resule = $client->recv();
|
||
|
echo $resule;
|