Table of Contents
Service End program acceptSever.php
Client program acceptClient.php
Code analysis
Home Backend Development PHP Tutorial Introduction to Accept blocking model in PHP network programming

Introduction to Accept blocking model in PHP network programming

Jul 28, 2018 am 10:33 AM
php

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

See all articles