php 구현 간단 한 다 중 프로 세 스 서버 클래스 전체 예시

이 실례 는 php 가 실현 하 는 간단 한 다 중 프로 세 스 서버 클래스 를 설명 한다.여러분 께 참고 하도록 공유 하 겠 습 니 다.구체 적 으로 는 다음 과 같 습 니 다.
php 가 작성 한 간단 한 다 중 프로 세 스 서버

<?php
class server
{
  public $port;
  public $ip;
  protected $server;
  public function __construct($ip = '0.0.0.0', $port)
  {
    $this->ip = $ip;
    $this->port = $port;
    $this->createSocket(); //        
  }
  public function listen($callback)
  {
    if(!is_callable($callback)){
      throw new Exception('    ,        ');
    }
    //             , fork       
    while ($client = socket_accept($this->server)) { //       ,          
      $buf = socket_read($client, 1024); //       
      $pid=pcntl_fork(); //     
      //               
      if ($pid == -1) {
        //    :          -1.
        die('could not fork');
      } else if ($pid) {
        //          ,             
        var_dump('   ',$pid);
        pcntl_wait($status); //       ,           。
      } else {
        //      $pid 0,              。
        //  
        if($this->checkRule("/sleep/i",$buf)){
          sleep(10);
          $this->response('  10S',$client);
          socket_close($client);
          return;
        }
        //    
        if(empty($this->checkRule("/GET\s(.*?)\sHTTP\/1.1/i",$buf))){
          socket_close($client);
          return;
        }
        //  
        $response= call_user_func($callback,$buf); //  $callback  
        $this->response($response,$client);
        usleep(1000); //     ,1000000     1 
        socket_close($client);
        exit(); //    
      }
    }
//    while (true) {
//      $client = socket_accept($this->server); //       ,          
//      $buf = socket_read($client, 1024); //       
//
//      //  
//      if($this->checkRule("/sleep/i",$buf)){
//        sleep(10);
//        $this->response('  10S',$client);
//        socket_close($client);
//        return;
//      }
//      //    
//      if(empty($this->checkRule("/GET\s(.*?)\sHTTP\/1.1/i",$buf))){
//        socket_close($client);
//        return;
//      }
//
//      //  
//      $response= call_user_func($callback,$buf); //  $callback  
//      $this->response($response,$client);
//      usleep(1000); //     ,1000000     1 
//      socket_close($client);
//
//    }
    socket_close($this->server);
  }
  //io   
  //epoll   
  //   
  protected function createSocket()
  {
    $this->server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    //bind
    socket_set_option($this->server, SOL_SOCKET, SO_REUSEADDR, 1); //      TIME_WAIT
    socket_bind($this->server, $this->ip, $this->port); //          
    socket_listen($this->server); //    
  }
  /**
   *     
   * @param $reg
   * @param $buf
   * @return mixed
   */
  protected function checkRule($reg,$buf){
    if(preg_match($reg,$buf,$matchs)){
      return $matchs;
    }
    return false;
  }
  //     
  public function request($buf){
    //1.   http    
//    if(preg_match("GET\s(.*?)\sHTTP/1.1",$buf,$matchs)){ //   http  
//      return true;
//    }else{
//      return false;
//    }
    //2.   /favicon.ico
    //3.      
  }
  protected function response($content,$client){
    //        ,    
    $string="HTTP/1.1 200 OK\r
"; $string.="Content-Type: text/html;charset=utf-8\r
"; $string.="Content-Length: ".strlen($content)."\r
\r
"; socket_write($client,$string.$content); } }
더 많은 PHP 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 논문 에서 말 한 것 이 여러분 의 PHP 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기