Server socket implemented by PHP

achieve

Related functions:, the  socket_create、socket_set_block、socket_bind、socket_listen、socket_accept、socket_read、socket_writespecific parameter descriptions of these functions are written in the PHP document in detail, so I won't repeat them here, here is just an introduction to how the server handles the request.

//Ensure that there will be no timeout when connecting to the client set_time_limit(0);//Set IP and port number $address = "127.0.0.1"; $port = 54321; $socketServer = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die ("socket_create() fail:". socket_strerror(socket_last_error()). "/n");//Set to blocking mode socket_set_block($socketServer) or die("socket_set_block() fail:". socket_strerror(socket_last_error()) . "/n");//Bind port $result = socket_bind($socketServer, $address, $port) or die("socket_bind() fail:". socket_strerror(socket_last_error()). "/n"); //Start listening $result = socket_listen($socketServer, 4) or die("socket_listen() fail:". Socket_strerror(socket_last_error()). "/N"); 
 do { 
    //Receive connection request and return a sub Socket To process the information between the client and the server  
    $sock = socket_accept($socketServer) or die("socket_accept() failed: reason: " . socket_strerror(socket_last_error()) . "/n");
    while($sock){ 
        //Read client data 
        echo "Read client data \n";
 
        $length = socket_read($sock, 4);
        $length = unpack('i', $length);
        echo "length:$length[1]   \n";
 
        $request = socket_read($sock, $length[1]);
        echo "$request:$request   \n";
 
 
        //数据传送 向客户端写入返回结果
        $msg = "this is response message \n";
        $msgLength = strlen($msg);
        $msgLength = pack('i', $msgLength);
        socket_write($sock, $msgLength);
        socket_write($sock, $msg, strlen($msg)) or die("socket_write() failed: reason: " . socket_strerror(socket_last_error()) ."/n");
        break;
    }
 } while (true);socket_close($socketServer);123456789101112131415161718192021222324252627282930313233343536373839

After receiving the client's request, it can be processed in combination with PHP's multi-threading, here is just a simple return string.

to sum up

The socketcommunication methods of all major languages are similar, and the processes on the server side are similar. In socketthe case of a ready-made high-performance communication framework, it is recommended not to implement it yourself, but you must understand how the underlying framework is implemented.

The above content hopes to help everyone. Many PHPers always encounter some problems and bottlenecks when they are advanced. There is no sense of direction when writing too much business code. I don’t know where to start to improve. I have compiled some information about this, including But not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripts, Docker, microservices, Nginx, etc. Knowledge points, advanced advanced dry goods, can be shared with everyone for free, you need to click here PHP Advanced Architect >>> actual combat video, interview documents from major manufacturers for free

Guess you like

Origin blog.csdn.net/ld17822307870/article/details/112727788