HTTP protocol Learning Notes---HTTP persistent connections and how to properly shut down an HTTP connection

Source: Internet
Author: User

One, persistent connection

What is a persistent connection? for the HTTP protocol, it is based on the request response model, when the client sends a request to the server, first establishes an HTTP connection, and the server responds to the client data, and the connection is closed.

When the client sends the next request, the HTTP connection needs to be re-established : A request response needs to consume an HTTP connection. The persistent connection is simply to establish a connection and then transmit multiple requests and responses on that connection.

Fields related to persistent connections

There is a connection header field in HTTP1.0, which is a skip header field. Connection:keep-alive, which indicates that you want this connection to be a persistent connection.

In HTTP1.1, the HTTP request that is established is persistent and connected by default. When client determines that data is no longer required to be sent to the server, it can close the connection by adding the Connection:closed field to the send header.

Second, how to improve the processing speed of HTTP requests

① Parallel Connection

The client requests the server to establish multiple HTTP connections at a time, sending a request on each HTTP connection so that multiple requests can be sent in parallel to get the response data.

For example, to access a Web page, it has 4 images, which are stored on different servers. That can send 4 HTTP requests to the server at the same time to get the four images, instead of sequentially sending an HTTP request to get the first picture first, then send the second HTTP request to get the second picture ....

Parallel connections, obviously increasing the pressure on the server side, requires the server to host a large number of HTTP requests.

② Persistent Connection

One of the biggest benefits of a persistent connection is that it greatly reduces the build-up of connections and the delay in closing.

The HTTP connection is built on top of the TCP protocol, which requires three handshakes to establish a TCP connection and four waves to close the TCP connection. It all takes time.

As mentioned in the 1th: A persistent connection is simply to establish a connection and then transmit multiple requests and responses on that connection. So, there is no need to make frequent connections, and the delay of connection establishment and shutdown is now eliminated.

Multiple request responses were completed within a single connection. However, a "disadvantage" is also seen here, and the request response is executed sequentially. Request 2 is sent only after the response of request 1 is received, which is where the persistent connection differs from the pipelined connection.

③ Piping Connection

What is a pipelined connection?

Obviously, a pipelined connection is required for persistent connection support. A pipelined connection is a "pipelined" way of sending a request on the basis of a persistent connection: you do not have to wait until the response of request 1 arrives at the client, you can send request 2 ....

How does the HTTP connection shut down correctly?

There are two issues to be aware of when an HTTP connection is closed: The HTTP connection is bidirectional, and the HTTP request is idempotent.

The connection is bidirectional

TCP connections are bidirectional, and both the sending and receiving sides of a TCP connection are: Send buffers and receive buffers, which correspond to the output channel and input channel.

In Java, the close () method of the JDK class library Java.net.Socket class closes the entire TCP connection, which means both the input channel and the output channel are closed.

In addition, the socket class provides two methods: Shutdowninput () and Shutdownoutput (), which are used to close the input channel, which is used to turn off the output channel.

If only one of the channels in the TCP connection is closed, it is called a semi-shutdown.

In general , it is always safe to close the output channel of the connection. when you're sure there's no data to send to each other, you can turn your output channel off.

The risk of shutting down the input channel may result in: The connection is end multiplicity.

For example, when the server just shuts down the input channel, the client initiates a request to the server, and the client receives a "connection is reset" message. In this case, the client's operating system empties the data inside the receive buffer in the client. If, at this point, the client's application has not yet taken the data out of the receive buffer, the data obtained from the previous send request is emptied!

Therefore, the correct way to close the HTTP connection is: (described in the client's view)

The client first shuts down its output channel (the client cannot turn off its input channel).

The client then periodically polls the state of its input channel (for example, when reading the data, is not the end of the stream that has been read), and if the end identifier of the stream is read, it means that the data sent by the server has been received.

In short, for both sides of the HTTP connection, when the transfer of data is no longer necessary, both sides have their own output channel shut down, and then read the stream in the input channel, if read to the end ( such as the stream Terminator returns-1), then the HTTP connection can gracefully shut down.

Resources:

The HTTP authoritative guide, chapter Fourth

"Graphic http"

HTTP protocol Learning Notes---HTTP persistent connections and how to properly shut down an HTTP connection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.