swoole查看子进程与主进程关系
【摘要】
ps aft|grep tcp.php
20688 pts/7 S+ 0:00 \_ grep --color=auto tcp.php20450 pts/4 Sl+ 0:00 \_ php tcp.php20451 pts/4 S+ 0:00 \_ php tcp.php2045...
ps aft|grep tcp.php
-
20688 pts/7 S+ 0:00 \_ grep --color=auto tcp.php
-
20450 pts/4 Sl+ 0:00 \_ php tcp.php
-
20451 pts/4 S+ 0:00 \_ php tcp.php
-
20453 pts/4 S+ 0:00 \_ php tcp.php
-
20454 pts/4 S+ 0:00 \_ php tcp.php
-
20455 pts/4 S+ 0:00 \_ php tcp.php
-
20456 pts/4 S+ 0:00 \_ php tcp.php
-
20457 pts/4 S+ 0:00 \_ php tcp.php
-
20458 pts/4 S+ 0:00 \_ php tcp.php
-
20459 pts/4 S+ 0:00 \_ php tcp.php
-
20460 pts/4 S+ 0:00 \_ php tcp.php
tcp.php
-
<?php
-
/**
-
*User: lxw
-
*Date: 2020-01-16
-
*/
-
//创建Server对象,监听 127.0.0.1:9501端口
-
$serv = new swoole_server("127.0.0.1", 9501);
-
-
$serv->set(array(
-
'worker_num'=>8, // worker 进程数 CPU 1~4 倍
-
'max_request'=>1000
-
));
-
-
/**
-
* $fd:客户端连接的唯一标识
-
* $reactor_id: 线程id
-
*/
-
//监听连接进入事件
-
-
//事件回调函数四种方法
-
//1.匿名函数
-
$a='hello';
-
$b='world';
-
$serv->on('Connect', function ($serv, $fd,$reactor_id) use($a,$b) {
-
echo "Client: {$reactor_id} - {$fd} Connect -{$a}-{$b}.\n";
-
});
-
-
//2.类静态方法
-
/*class A{
-
static function client ($serv, $fd,$reactor_id) {
-
echo "Client: {$reactor_id} - {$fd} Connect.\n";
-
}
-
}
-
$serv->on('Connect', 'A::Client');*/
-
//$serv->on('Connect', array('A','client'));
-
-
-
//3.函数
-
//function my_func($serv, $fd,$reactor_id){
-
// echo "Client: {$reactor_id} - {$fd} Connect 22.\n";
-
//}
-
//$serv->on('Connect', 'my_func');
-
-
//4.对象方法
-
/*class B{
-
function client($serv, $fd,$reactor_id){
-
echo "Client: {$reactor_id} - {$fd} Connect 22.\n";
-
}
-
}
-
$obj=new B();
-
$serv->on('Connect', array($obj,'client'));*/
-
-
-
//监听数据接收事件
-
$serv->on('Receive', function ($serv, $fd, $reactor_id, $data) {
-
$serv->send($fd, "Server: {$reactor_id} - {$fd} ".$data);
-
});
-
-
//监听连接关闭事件
-
$serv->on('Close', function ($serv, $fd) {
-
echo "Client: Close.\n";
-
});
-
-
//启动服务器
-
$serv->start();
-
-
-
//使用 telnet 127.0.0.1 9501
-
// netstat -anp|grep 9501
tcp_client.php
-
<?php
-
/**
-
*User: lxw
-
*Date: 2020-01-17
-
*/
-
-
//连接 TCP 服务
-
$client=new swoole_client(SWOOLE_SOCK_TCP);
-
-
if (!$client->connect('0.0.0.0',9501)){
-
echo '连接失败';
-
exit();
-
}
-
-
//php cli 常量
-
fwrite(STDOUT,'请输入消息:');
-
$msg=trim(fgets(STDIN));
-
-
//发送消息给tcp server 服务器
-
$client->send($msg);
-
-
//接受来自server 的数据
-
$result=$client->recv();
-
echo $result;
文章来源: blog.csdn.net,作者:lxw1844912514,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/lxw1844912514/article/details/106627637
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)