我们来用swoole这个php扩展实现一个简单的客户端和服务器,并且让它们进行简单的交流
首先,我们先来实现客户端(命名为:client.php):
connect('127.0.0.1', 9501, -1))
{
exit("connect failed. Error: {$client->errCode}\n");
}
fwrite(STDOUT, 'Enter the message you want to send, and you can enter the no to quit it '."\n");
while(true) {
fwrite(STDOUT, 'client: ');
$info = fgets(STDIN);
if('no'."\n" === $info) {
break;
}
$client->send($info);
$recive = $client->recv();
echo 'server: '.$recive."\n";
}
$client->close();
然后,再来实现一个简单的服务器(命名为:server.php):
on('receive', function($server, $fd, $from_id, $data) {
if('what is your name'."\n" === $data) {
$server->send($fd, 'My name is huanghantao');
}
elseif('where are you from'."\n" === $data) {
$server->send($fd, 'I am from China');
}
elseif('how old are you'."\n" === $data) {
$server->send($fd, 'I am 20');
}
else{
$apiKey = "7e97323fbb5b4c56aetc7f1ff5231b20";
$apiURL = "http://www.tuling123.com/openapi/api?key=KEY&info=INFO";
// 设置报文头, 构建请求报文
header("Content-type: text/html; charset=utf-8");
$reqInfo = $data;
$url = str_replace("INFO", $reqInfo, str_replace("KEY", $apiKey, $apiURL));
$res = json_decode(file_get_contents($url), true);
$server->send($fd, $res['text']);
}
});
$server->start();
接下来,进入这两个文件所在的目录
然后再执行这server.php文件来启动服务器(注意: 一定要先启动服务器再来启动客户端):
php server.php
此时,就开始监听环回地址127.0.0.1的9501端口
接着,我们执行client.php这个文件:
php client.php
这样,客户端和服务器就建立了一个TCP网络连接
现在,我们就可以在客户端和服务器进行通信了,现在我们来试一试,很好玩的:
最后,我们输入no来让客户端主动断开与服务器的连接