swoole查看子进程与主进程关系
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;
赞赏码
![](https://files.cnblogs.com/files/lxwphp/zhifubao2.bmp)
![](https://files.cnblogs.com/files/lxwphp/weixin.bmp)
非学,无以致疑;非问,无以广识