当前位置:首页 > PHP教程 > php类库 > 列表

php实现的简单多进程服务器类完整示例

发布:smiling 来源: PHP粉丝网  添加日期:2022-02-09 11:54:40 浏览: 评论:0 

这篇文章主要介绍了php实现的简单多进程服务器类,结合完整实例形式分析了PHP多进程服务器数据传输、响应、处理等相关操作技巧,需要的朋友可以参考下。

本文实例讲述了php实现的简单多进程服务器类,分享给大家供大家参考,具体如下:

php写的一个简单的多进程服务器。

  1. <?php 
  2. class server 
  3. { 
  4.   public $port; 
  5.   public $ip; 
  6.   protected $server; 
  7.   public function __construct($ip = '0.0.0.0', $port) 
  8.   { 
  9.     $this->ip = $ip; 
  10.     $this->port = $port; 
  11.     $this->createSocket(); //创建一个通讯节点 
  12.   } 
  13.   public function listen($callback) 
  14.   { 
  15.     if(!is_callable($callback)){ 
  16.       throw new Exception('不是闭包,请传递正确的参数'); 
  17.     } 
  18.     //只要我们接收到客户端的数据,就fork一个子进程处理 
  19.     while ($client = socket_accept($this->server)) { //等待客户端接入,返回的是客户端的连接 
  20.       $buf = socket_read($client, 1024); //读取客户端内容 
  21.       $pid=pcntl_fork(); //创建子进程 
  22.       //父进程和子进程都会执行下面代码 
  23.       if ($pid == -1) { 
  24.         //错误处理:创建子进程失败时返回-1. 
  25.         die('could not fork'); 
  26.       } else if ($pid) { 
  27.         //父进程会得到子进程号,所以这里是父进程执行的逻辑 
  28.         var_dump('父进程',$pid); 
  29.         pcntl_wait($status); //等待子进程中断,防止子进程成为僵尸进程。 
  30.       } else { 
  31.         //子进程得到的$pid为0, 所以这里是子进程执行的逻辑。 
  32.         //睡眠 
  33.         if($this->checkRule("/sleep/i",$buf)){ 
  34.           sleep(10); 
  35.           $this->response('休眠10S',$client); 
  36.           socket_close($client); 
  37.           return; 
  38.         } 
  39.         //请求过滤 
  40.         if(emptyempty($this->checkRule("/GET\s(.*?)\sHTTP\/1.1/i",$buf))){ 
  41.           socket_close($client); 
  42.           return; 
  43.         } 
  44.         //响应 
  45.         $response= call_user_func($callback,$buf); //回调$callback函数 
  46.         $this->response($response,$client); 
  47.         usleep(1000); //微妙为单位,1000000 微妙等于1秒 
  48.         socket_close($client); 
  49.         exit(); //直接退出 
  50.       } 
  51.     } 
  52. //    while (true) { 
  53. //      $client = socket_accept($this->server); //等待客户端接入,返回的是客户端的连接 
  54. //      $buf = socket_read($client, 1024); //读取客户端内容 
  55. // 
  56. //      //睡眠 
  57. //      if($this->checkRule("/sleep/i",$buf)){ 
  58. //        sleep(10); 
  59. //        $this->response('休眠10S',$client); 
  60. //        socket_close($client); 
  61. //        return; 
  62. //      } 
  63. //      //请求过滤 
  64. //      if(empty($this->checkRule("/GET\s(.*?)\sHTTP\/1.1/i",$buf))){ 
  65. //        socket_close($client); 
  66. //        return; 
  67. //      } 
  68. // 
  69. //      //响应 
  70. //      $response= call_user_func($callback,$buf); //回调$callback函数 
  71. //      $this->response($response,$client); 
  72. //      usleep(1000); //微妙为单位,1000000 微妙等于1秒 
  73. //      socket_close($client); 
  74. // 
  75. //    } 
  76.     socket_close($this->server); 
  77.   } 
  78.   //io 复用 
  79.   //epoll 模型 
  80.   //多进程 
  81.   protected function createSocket() 
  82.   { 
  83.     $this->server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 
  84.     //bind 
  85.     socket_set_option($this->server, SOL_SOCKET, SO_REUSEADDR, 1); //复用还处于 TIME_WAIT 
  86.     socket_bind($this->server, $this->ip, $this->port); //细节性的处理自行完成 
  87.     socket_listen($this->server); //开始监听 
  88.   } 
  89.   /** 
  90.    * 协议过滤 
  91.    * @param $reg 
  92.    * @param $buf 
  93.    * @return mixed 
  94.    */ 
  95.   protected function checkRule($reg,$buf){ 
  96.     if(preg_match($reg,$buf,$matchs)){ 
  97.       return $matchs; 
  98.     } 
  99.     return false; 
  100.   } 
  101.   //请求处理类 
  102.   public function request($buf){ 
  103.     //1.只允许http协议访问 
  104. //    if(preg_match("GET\s(.*?)\sHTTP/1.1",$buf,$matchs)){ //匹配到http协议 
  105. //      return true; 
  106. //    }else{ 
  107. //      return false; 
  108. //    } 
  109.     //2.过滤掉/favicon.ico 
  110.     //3.获取请求信息 
  111.   } 
  112.   protected function response($content,$client){ 
  113.     //返回数据给客户端,响应处理 
  114.     $string="HTTP/1.1 200 OK\r\n"; 
  115.     $string.="Content-Type: text/html;charset=utf-8\r\n"; 
  116.     $string.="Content-Length: ".strlen($content)."\r\n\r\n"; 
  117.     socket_write($client,$string.$content); 
  118.   } 
  119. }

Tags: php多进程服务器类

分享到: