Is TCP applicable to the WAN environment?

Source: Internet
Author: User
Tags socket error

Is TCP applicable to the WAN environment?

1. Background information

A long time ago, I heard a colleague from the department say that the Http protocol is applicable to the WAN, but the TCP protocol is not applicable to the WAN, because the Http protocol is a short connection and the TCP protocol is a persistent connection, large overhead!

In fact, a careful analysis shows that such a speech is not true. The Http protocol itself is based on the TCP protocol. before initiating an Http request, the client needs to establish a TCP connection with the server through three handshakes.

The following sections are taken from the network and finally the conclusion is given.

2. persistent connection and short connection

Operation Process of persistent connection and short connection:

The procedure for a short connection is to establish a connection-data transmission-close the connection... establish a connection-data transmission-close the connection;

The procedure for a persistent connection is to establish a connection-data transmission... (keep the connection)... data transmission-close the connection.

Use Time of persistent connection and short connection:

Persistent connections are often used for frequent operations, point-to-point communication, and the number of connections cannot be too large. Three handshakes are required for the establishment of each TCP connection, and four handshakes are required for the disconnection of each TCP connection. If you need to establish a connection for each operation and then perform the operation, the processing speed will be reduced. Therefore, you can directly send data during the next operation of each operation without establishing a TCP connection. For example, if a database is connected with a persistent connection, frequent communication with a short connection may cause a socket error, and frequent socket creation is also a waste of resources.

Short connection: http Services of web sites generally use short connections. Because persistent connections consume certain resources for servers. Connections to thousands or even hundreds of millions of clients such as web sites frequently use short connections to save some resources. Imagine how much pressure the server would be under if we were using persistent connections with thousands of users and each user had a connection. Therefore, a large number of concurrent connections are required, but each user does not need to perform frequent operations.

In short, the choice of persistent connection and short connection depends on the needs.

3. Comparison between HTTP 1.1 and HTTP 1.0

A web site may receive millions of user requests every day. To improve system efficiency, HTTP 1.0 requires that the browser and server only maintain a short connection, each browser request requires a TCP connection with the server. The server disconnects the TCP connection immediately after processing the request. The server does not track or record the previous request. However, this also causes some performance defects. For example, a webpage file containing many images does not contain real image data content, the URL of these images is specified. When a WEB browser accesses this webpage file, the browser must first send a request for this webpage file, when the browser parses the HTML content in the webpage document returned by the WEB server, after the Image Tag is found, the browser sends an image download request to the server again based on the URL specified by the src attribute in the tag, as shown in Figure 3.3.

 

 

 

 

Fig 3.3

Obviously, the whole process of accessing a webpage file containing many images contains multiple requests and responses. Each request and response requires a separate connection, each connection transmits only one document and image, and the last and next requests are completely separated. Even if the image files are small, it is a relatively time-consuming process to establish and close the connection between the client and the server, and seriously affects the performance of the client and server. When a webpage file contains the Applet, JavaScript file, CSS file, and other content, the above situation also occurs.

To overcome the defect of HTTP 1.0, HTTP 1.1 supports persistent connections. Multiple HTTP requests and responses can be sent over a TCP connection, reducing the consumption and delay of establishing and disabling connections. Multiple requests and responses to a webpage file containing many images can be transmitted in one connection, but each individual webpage file's request and response still need to use their own connection. HTTP 1.1 also allows the client to send the next request without waiting for the result of the previous request. However, the server must send the response in sequence according to the order in which the client request is received, to ensure that the client can differentiate the response content of each request, which significantly reduces the time required for the entire download process. Information exchange between clients and servers based on HTTP 1.1, as shown in Figure 3.4.

 

 

Fig 3.4

It can be seen that HTTP 1.1 inherits the advantages of HTTP 1.0 and also overcomes the performance problems of HTTP 1.0. In addition, HTTP 1.1 also improves and expands HTTP 1.0 by adding more request headers and response headers. For example, because HTTP1.0 does not support the Host request header field, the WEB browser cannot use the Host header name to indicate which WEB site on the server to access, in this way, you cannot use the WEB server to configure multiple virtual WEB sites on the same IP address and port number. After the Host request header field is added to HTTP 1.1, the WEB browser can use the Host header to specify which WEB site on the server to access, this allows you to create multiple virtual WEB sites by using different host names on the same IP address and port number on a WEB server. For HTTP 1.1 persistent connections, you also need to add new request headers for implementation. For example, when the Connection request header value is Keep-Alive, the client notifies the server to keep the Connection after returning the result of this request. When the value of the Connection request header is close, the client notifies the server to close the Connection after returning the result of this request. HTTP 1.1 also provides request headers and Response Headers related to identity authentication, status management, and Cache caching.

The old standard of HTTP protocol is HTTP/1.0. Currently, the most common standard is HTTP/1.1. HTTP/1.1 is an upgrade based on HTTP/1.0. It adds some features and is fully compatible with HTTP/1.0. HTTP/1.0 does not support resumable upload of files. Currently, most Web servers use HTTP/1.1.

RANGE: bytes is the new content in HTTP/1.1. Each time HTTP/1.0 transfers a file, it starts from the file header, that is, it starts at 0 bytes. RANGE: bytes = XXXX indicates that the server is required to transmit the file from the XXXX byte. This is what we usually call resumable data transfer!

4. HTTP 1.1 persistent connections and HTTP 1.0 transient connections

1. Background

KeepAlive is generally called a persistent connection. The benefit of KeepAlive is that it can reduce the overhead of tcp connections, which is more effective for requests with short response bodies. At the same time, it can provide good session support for interactive applications using HTTP protocol.

2. Principles of KeepAlive

Both HTTP1.0 and HTTP1.1 support KeepAlive. HTTP1.0 requires "Connection: keep-alive" header to be added to the request, whereas HTTP1.1 supports this header by default.

The data interaction process supported by HTTP1.0 KeepAlive is as follows:

A) when the Client sends a request, the HTTP Version of the request is 1.0. At the same time, the request contains a header: "Connection: keep-alive".

B) when the Web Server receives the HTTP protocol 1.0 and "Connection: keep-alive" in the request, it is considered to be a persistent Connection request and will also add "Connection: keep-alive ". The established tcp connection is not closed at the same time.

C) when the Client receives a response from the Web Server that contains "Connection: keep-alive", it considers it a persistent Connection and does not close the tcp Connection. Use the tcp connection to send the request again. (Jump to ))

The data interaction process supported by HTTP1.1 KeepAlive is as follows:

A) when the Client sends a request, the HTTP Version of the request is 1.1.

B) when the HTTP protocol in the request is 1.1, the Web Server considers it a persistent Connection request and adds "Connection: keep-alive" to the header of the response. The established tcp connection will not be closed.

C) when the Client receives a response from the Web Server that contains "Connection: keep-alive", it considers it a persistent Connection and does not close the tcp Connection. Use the tcp connection to send the request again. (Jump to ))

5. Summary

It can be inferred that "TCP or persistent connection is not applicable to Wan" is not true.

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.