swoole创建tcp服务器的方法(代码示例)
来源:不言
发布时间:2019-01-16 15:01:35
阅读量:2009
本篇文章给大家带来的内容是关于swoole创建tcp服务器的方法(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
server.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php
$serv = new swoole_server('127.0.0.1', 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();
|
1.执行程序,启动服务器
2. 启动成功后,netstat 查看
1 2 | $ sudo netstat -ntlp | grep php
tcp 0 0 127.0.0.1:9501 0.0.0.0:* LISTEN 21314/php
|
3. telnet连接服务器
1 2 3 4 | $ telnet 127.0.0.1 9501Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.hello
Server: hello
|
退出telnet:shift+],quit
4. 结束工作进程:kill 主进程ID