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

Introduction to Accept blocking model in PHP network programming

不言
不言 Original
2018-07-28 10:33:44 2050browse
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

 " . $ct_data . PHP_EOL; fwrite($conn, "Received $ct_size byte data./r/n"); fclose($conn); } fclose($socket); } } new SocketServer(2000);

Client program acceptClient.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:
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. Causedeadlock. 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 atimeoutof 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:
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