多进程监听同个端口及单进程监听多个端口的php版本实现
2016-11-06 本文已影响714人
beliger00
最近在看nginx设计原理时思考到两个问题,便是:
- 多个进程能否监听同个端口?
- 单个进程能否监听多个端口?
当然随着学习的深入,答案均是肯定的,在这个过程中笔者为了验证,用php写了两个例子,在这里分享出来,供有需要的php同学学习跟理解。
在分享例子之前,需要先介绍两个php在socket编程中常用的扩展,pcntl和libevent:
1. pcntl
php本身并不支持多进程,但通过扩展pcntl便可以实现fork功能,fork编程的大概原理是,每次调用fork函数,操作系统就会产生一个子进程,儿子进程所有的堆栈信息都是原封不动复制父进程的,而在fork之后,父进程与子进程实际上是相互独立的,父子进程不会相互影响。也就是说,fork调用位置之前的所有变量,父进程和子进程是一样的,但fork之后则取决于各自的动作,且数据也是独立的;因为数据已经完整的复制给了子进程。而唯一能够区分父子进程的方法就是判断fork的返回值。如果为0,表示是子进程,如果为正数,表示为父进程,且该正数为子进程的PID(进程号),而如果是-1,表示子进程创建失败。
2. libevent
linux网络编程中有三大事件处理,IO(socket)、信号和定时器,理解并处理好这三者,linux网络编程就理解了一半,而libevent则是对这三者处理提供了一个很好的封装,大大简化了socket编程中事件处理的难度,非常推荐对这块感兴趣的同学去深入学习。
一、多个进程监听同个端口
<?php
/**
*
*/
class Server
{
protected $ip = '127.0.0.1';
protected $port = 5000;
protected $sock = null;
public function main()
{
if(($this->sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) < 0) {
echo "socket_create() 失败的原因是:".socket_strerror($sock)."\n";
return ;
}
if(($ret = socket_bind($this->sock,$this->ip,$this->port)) < 0) {
echo "socket_bind() 失败的原因是:".socket_strerror($ret)."\n";
return ;
}
if(($ret = socket_listen($this->sock,4)) < 0) {
echo "socket_listen() 失败的原因是:".socket_strerror($ret)."\n";
return ;
}
for ($i=0; $i<3; $i++)
{
$pid = pcntl_fork();
if (-1 === $pid) {
throw new Exception("fork fail");
} elseif (0 === $pid) {
echo "fork pid:".getmypid()."\n";
while (1) {
if(($msgsock = socket_accept($this->sock)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . " ,pid: ".getmypid()."\n";
break;
}else{
$msg ="测试成功 ! \n";
echo $msg."pid: ".getmypid()."\n";
socket_write($msgsock, $msg, strlen($msg));
}
}
}
}
while(1)
{
$status = 0;
$pid = pcntl_wait($status,WUNTRACED);
if($pid > 0)
{
echo "pid:$pid exit,status:$status";
}
}
}
}
$server = new Server();
$server->main();
二、单个进程监听多个端口
<?php
/**
*
*/
class Server
{
protected $socks = array();
protected $event_base = null;
protected $events = array();
public function __construct()
{
$this->event_base = event_base_new();
}
protected function acceptConnect($sock)
{
echo "acceptConnect pid:".getmypid()."\n";
//sleep(5);
if(($msgsock = socket_accept($sock)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . " ,pid: ".getmypid()."\n";
//break;
}else{
$msg ="测试成功,sock:$sock ! \n";
echo $msg."pid: ".getmypid()."\n";
socket_write($msgsock, $msg, strlen($msg));
socket_close($msgsock);
}
}
protected function addEvent($sock,$callback)
{
$event = event_new();
if (!event_set($event, $sock, EV_READ|EV_PERSIST, $callback, null)) {
echo "event_set faild,pid:".getmypid()."\n";
return ;
}
if (!event_base_set($event,$this->event_base)) {
echo "event_base_set faild,pid:".getmypid()."\n";
return ;
}
if (!event_add($event)) {
echo "event_add faild,pid:".getmypid()."\n";
return ;
}
$this->event[] = $event;
}
public function listen($ip = '127.0.0.1',$port = '5000')
{
if(($sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) < 0) {
echo "socket_create() 失败的原因是:".socket_strerror($sock)."\n";
return ;
}
if(($ret = socket_bind($sock,$ip,$port)) < 0) {
echo "socket_bind() 失败的原因是:".socket_strerror($ret)."\n";
return ;
}
if(($ret = socket_listen($sock,4)) < 0) {
echo "socket_listen() 失败的原因是:".socket_strerror($ret)."\n";
return ;
}
$this->socks[] = $sock;
}
public function main()
{
if($this->event_base == null)
{
echo "event base null";
return ;
}
echo "event base:".$this->event_base."\n";
foreach ($this->socks as $sock) {
echo "sock:$sock\n";
$this->addEvent($sock,array($this,'acceptConnect'));
}
echo "libevent success,pid:".getmypid()."\n";
$result = event_base_loop($this->event_base);
echo "event loop result:$result";
}
}
$server = new Server();
$server->listen('127.0.0.1','5000');
$server->listen('127.0.0.1','5001');
$server->main();