The implementation of UNIX socket communication with spool

Time:2021-1-16

Server

bind("/tmp/server.sock");
$socket->listen(2048);

go(function ()use($socket){
    while(true) {
        echo "Accept: \n";
        $client = $socket->accept();
        if ($client === false) {
            var_dump($socket->errCode);
        } else {
            Swoole\Event::add($client,function($client){
                if(!$client->checkLiveness()){
                    $client->close();
                    Swoole\Event::del($client);
                    return;
                }
                echo $client->fd."****".$client->recv().PHP_EOL;
                $client->send("world");
            });
        }
    }
});

client

connect("/tmp/server.sock");
    while ($retval)
    {
        $socket->send("hello");
        $data = $socket->recv();
        echo "server recv: ".$data.PHP_EOL;
        if (empty($data)) {
            $socket->close();
            break;
        }
        co::sleep(1.0);
    }
});