[Linux] multi-process programming monitor a network port

SO_REUSEPORT support multiple processes or threads to bind to the same port

Each process can create your own socket, bind, listen, accept the same address and port, each is separate and equal. Let the same multi-process monitor port, each process accept socket fdis different, there is a new connection is established, the kernel will wake up to a process accept, and ensure a balanced sexual awakening

<?php
$context=stream_context_create();
stream_context_set_option($context, 'socket', 'so_reuseport', 1);
for($i=0;$i<2;$i++){
    $pid = pcntl_fork();
    if($pid == 0){
        while(true){
            $socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr,STREAM_SERVER_BIND | STREAM_SERVER_LISTEN,$context);
            while ($conn = @stream_socket_accept($socket,5)) {
                fwrite($conn, getmypid().':时间:' . date('Y-m-d H:i:s') . "\n");
                fclose($conn);
            }
            fclose($socket);
        }
        
    }
}

while(1){
    $pid = pcntl_wait($status);
    var_dump($pid,$status);
    sleep(1);
}

 

Guess you like

Origin www.cnblogs.com/taoshihan/p/12014162.html