php long connections and short connection distinction

  

Short connection

Connection -> Data transmission -> close the connection (recommended study: PHP Programming from entry to the master)

For example, HTTP is stateless short link, the browser and the server once for each HTTP operation, establish a connection, but the task will end disconnected.

Concrete is a browser client to initiate and establish a TCP connection -> client to send HttpRequest message -> server receives a message -> server handle and send a message to the front HttpResponse, calling socket.close method immediately after the transmission is completed -> client receives the response message -> client will receive a final signal end server breaks the TCP connection -> client tears down the TCP connection, the specific method is to call close.

It can be said: short connection means after SOCKET connection, sending the data received immediately disconnected.

Because the data is received after the connection is disconnected, the data receiving processing each time there will be no contact. This is also one of the reasons the stateless protocol of HTTP.

Long connection

Connection -> transmit data -> remain connected -> transmit data -> ...........-> until one closes the connection, multi-client connection is closed.

After establishing a long connection refers to the SOCKET connection regardless of whether the connection is maintained, but less secure.

Every time we access a PHP script, is when all of the PHP script execution is completed, we get back the results. If we need a script continued to run, then we have by php a long way connection to achieve operational purposes.

General environment for php apache + php + linux, but because apache has a time limit for php connection, the connection time is generally more than apache server will automatically cut off the connection.

In this case relatively simple and convenient way is to set set_time_limit (0) (of course, can also be set in the php.ini configuration file, but such an impact on the environment in php page, after all, we do not think can be long for all connections time connection, affect service performance)

Each PHP script limits the execution time, so we need to set up a script by set_time_limit execution time is infinite, then use flush () and ob_flush () to remove the server buffer, the output of the script at any time of the return value.

<? Php header ( "Content-Type: text / plain"); set_time_limit (0); while (true) {// script performed continuously flush (); ob_flush (); sleep (5);}?>

When we perform, every 5 seconds, we will execute once, by this method, we can do a lot of features, such as background monitoring program, perform regular functions, log analysis, data cleansing and other time-consuming operation.

Guess you like

Origin www.cnblogs.com/68xi/p/11528078.html