


Introduction to Accept blocking model in PHP network programming
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 = ''; # 接受的数据 $buffer = ''; # 分段读取数据 $id++; echo "Client $id come" . PHP_EOL; # 持续监听 while (!preg_match('{/r/n}', $buffer)) { // 没有读到结束符,继续读 // if (feof($conn)) break; // 防止 popen 和 fread 的 bug 导致的死循环 $buffer = fread($conn, 1024); echo 'R' . PHP_EOL; # 打印读的次数 $ct += strlen($buffer); $ct_data .= preg_replace('{/r/n}', '', $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);
Client program acceptClient.php
<?php # 日志记录 function debug ($msg) { error_log($msg, 3, './socket.log'); } if ($argv[1]) { $socket_client = stream_socket_client('tcp://0.0.0.0:2000', $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 'W'; // 打印写的次数 # 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__." '{$i}:test'", 'r'); // } // foreach ($phArr as $ph) { // pclose($ph); // } for ($i = 0; $i < 10; $i++) { system("php ".__FILE__." '{$i}:test'"); # 这里等于 php "当前文件" "脚本参数" } }
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:
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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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 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.

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

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,
