So you can judge the end of a request based on EOF, the following code (PHP) is common:
Copy the Code code as follows:
$FP is a handle produced by Fsockopen ()
while (!feof ($fp)) {
Echo fgets ($FP);
}
(Note: The short connection mode is marked with "Connection:close" in the head, and the long connection is marked with "connection:keep-alive". Currently http/1.0 uses short connections by default, and http/1.1 uses long connections by default. )
The long connection (also known as persistent connection) mode of HTTP after sending the data after the server does not disconnect, but to keep the next HTTP request to use, so the benefits of long connection is obvious, by sharing a TCP connection to save the subsequent request to establish/disconnect the cost of the connection. EOF is not sent until the end of the TCP connection (timeout or error), so we cannot use the method above to determine the end of an HTTP request. This is also an issue that you will encounter when using long connections. There are two main methods of judging today:
(1) Based on the Content-length field in the header. This field indicates the length of the body, and we can determine the end of the text by receiving the specified length of the character.
(2) In the absence of content-length, according to Transfer-encoding. Sometimes the server cannot determine the size of the body, because the body may be generated dynamically, so it does not provide content-length, but instead uses chunk encoding to send the body one piece at a time. Each chunk block consists of the head and the body two parts, the head is specified by a 16 binary number of the length of the body, and finally a 0-length chunk block to represent the end of the entire HTTP body.
Here I use PHP to achieve the content-length when the way to judge:
1. Get Content-length value
Copy the Code code as follows:
$length = 0;
$line = ";
while ($line!== "\ r \ n") {
$line = fgets ($FP);
if (substr ($line, 0,) = = = = ' Content-length: ') {
$length = Intval (substr ($line, 16));
}
}
2. Get the text
Copy the Code code as follows:
$sum = 0;
while ($sum < $length) {
$line = fgets ($FP);
$sum + = strlen ($line);
Echo $line;
}
The above describes the implementation code for the end of the HTTP request for the Galaxy S4 active Judgment keep-alive mode, including the contents of the Galaxy S4 active, which hopefully helps friends interested in PHP tutorials.