Home > Backend Development > PHP Tutorial > Introduction to Accept blocking model in PHP network programming

Introduction to Accept blocking model in PHP network programming

不言
Release: 2023-04-03 13:32:02
Original
2193 people have browsed it
The content of this article is to share with you an introduction to the Accept blocking model in PHP network programming. The content is very detailed. Friends in need can refer to it. I hope it can help everyone.

Accept blocking model is a relatively old model, but it contains a lot of interesting knowledge, such as blocking/non-blocking, locks, timeout retransmission...

Service End program acceptSever.php

<?php set_time_limit(0);  # 设置脚本执行时间无限制

class SocketServer 
{
    private static $socket;
    function SocketServer($port) 
    {
        global $errno, $errstr;
        if ($port < 1024) {
            die("Port must be a number which bigger than 1024/n");
        }
        
        $socket = stream_socket_server("tcp://0.0.0.0:{$port}", $errno, $errstr);
        if (!$socket) die("$errstr ($errno)");
        
        
        while ($conn = stream_socket_accept($socket, -1)) { // 这样设置不超时才有用
            static $id = 0; # 进程 id
            static $ct = 0; # 接收数据的长度  
            $ct_last = $ct; 
            $ct_data = &#39;&#39;; # 接受的数据
            $buffer = &#39;&#39;;  # 分段读取数据
        
            $id++; 
            echo "Client $id come" . PHP_EOL;
            
            # 持续监听
            while (!preg_match(&#39;{/r/n}&#39;, $buffer)) { // 没有读到结束符,继续读
//                if (feof($conn)) break; // 防止 popen 和 fread 的 bug 导致的死循环
                $buffer = fread($conn, 1024);
                echo &#39;R&#39; . PHP_EOL; #  打印读的次数
                $ct += strlen($buffer);
                $ct_data .= preg_replace(&#39;{/r/n}&#39;, &#39;&#39;, $buffer);
            }
            $ct_size = ($ct - $ct_last) * 8;
            echo "[$id] " . __METHOD__ . " > " . $ct_data . PHP_EOL;
            fwrite($conn, "Received $ct_size byte data./r/n");
            fclose($conn);
        }
        
        fclose($socket);
    }
}
new SocketServer(2000);
Copy after login

Client program acceptClient.php

<?php # 日志记录
function debug ($msg)
{
    error_log($msg, 3, &#39;./socket.log&#39;);
}

if ($argv[1]) {
    
    $socket_client = stream_socket_client(&#39;tcp://0.0.0.0:2000&#39;, $errno, $errstr, 30);
    
    /*    设置脚本为非阻塞    */
#    stream_set_blocking($socket_client, 0);

    /*    设置脚本超时时间    */
#    stream_set_timeout($socket_client, 0, 100000);
    
    if (!$socket_client) {
        die("$errstr ($errno)");
    } else {
        # 填充容器
        $msg = trim($argv[1]);

        for ($i = 0; $i < 10; $i++) {
            $res = fwrite($socket_client, "$msg($i)");
            usleep(100000);
            echo &#39;W&#39;; // 打印写的次数
#            debug(fread($socket_client, 1024)); // 将产生死锁,因为 fread 在阻塞模式下未读到数据时将等待
        }
        fwrite($socket_client, "/r/n"); // 传输结束符
        # 记录日志
        debug(fread($socket_client, 1024));
        fclose($socket_client);
    }
}
else {
    
//    $phArr = array();
//    for ($i = 0; $i < 10; $i++) {
//        $phArr[$i] = popen("php ".__FILE__." &#39;{$i}:test&#39;", &#39;r&#39;);
//    }
//    foreach ($phArr as $ph) {
//        pclose($ph);
//    }
    
    for ($i = 0; $i < 10; $i++) {
        system("php ".__FILE__." &#39;{$i}:test&#39;");    # 这里等于 php "当前文件" "脚本参数"
    }
}
Copy after login

Code analysis

First, explain the above code logic: client acceptClient.php sends data in a loop , and finally send the terminator; the server accept Server.php uses the accept blocking method to receive the socket connection, and then receives data in a loop until the terminator is received, and returns the result data (number of bytes received). The client receives the data returned by the server. , write to the log. Although the logic is very simple, there are several situations worth analyzing:

A> By default, when running php socket_client.php test, the client will print 10 W, and the server will print several R, followed by For the received data, socket.log records the reception result data returned by the server. The effect is as follows:
Introduction to Accept blocking model in PHP network programming
This situation is easy to understand and will not be repeated. Then, use the telnet command to open multiple clients at the same time. You will find that the server only processes one client at a time, as shown in the figure:
Introduction to Accept blocking model in PHP network programming
Others need to be queued later. "; This is the characteristic of blocking IO. The weaknesses of this mode are obvious and the efficiency is extremely low.

B> Only open the comment code on line 29 of socket_client.php and run php socket_client.php test again. The client prints a W and the server also prints an R. After that, both programs are stuck. Why is this? After analyzing the logic, you will find that this is because the client wants to return data to the server before sending the terminator; and the server, because it has not received the terminator, is also asking the client for the terminator. Cause deadlock. The reason why only one W and R is typed is because fread is blocking by default. To solve this deadlock, you must open the comment code on line 17 of socket_client.php and set a timeout of 0.1 seconds for the socket. If you run it again, you will find that a W and R appear every 0.1 seconds and then end normally. The reception result data returned by the server is also recorded normally. It can be seen that fread is blocking by default. We must pay special attention when programming. If the timeout is not set, deadlock will easily occur.

C> Open only 14 lines of comments, set the script to non-blocking, and run php socket_client.php test. The result is basically the same as case A. The only difference is that the socket.log does not record the return data. This is because when In our non-blocking mode, the client can continue execution without waiting for the response result from the server. When debug is executed, the read data is still empty, so socket.log is also empty. Here you can see the difference between the client running in blocking and non-blocking modes. Of course, when the client does not care about accepting the results, the non-blocking mode can be used to obtain maximum efficiency.

D> Running php socket_client.php is to run the above logic 10 times continuously. There is no problem with this; but what is very strange is that if you use 39-45 lines of code, use popen to open 10 processes at the same time. , will cause an infinite loop on the server side, which is very weird! Later, after investigation, it was found that as long as the connection created by the process opened with popen will cause fread or socket_read to error and directly return an empty string, resulting in an infinite loop, after checking the PHP source code, it was found that PHP's popen and fread functions are not native to C at all. , a large amount of php_stream_* implementation logic has been inserted into it. It is initially estimated that it is caused by the Socket connection interruption caused by a bug in it. The solution is to open the 33 lines of code in socket_server.php. If the connection is interrupted, jump out of the loop, but In this way, a lot of data will be lost, and this issue requires special attention!

Related recommendations:

Basic applications of PHP and Apache

What are PHP regular expressions? How to use PHP regular expressions (with code)

The above is the detailed content of Introduction to Accept blocking model in PHP network programming. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template