TCP protocol corresponds to the transport layer, while HTTP protocol corresponds to the application layer. In essence, the two are not comparable. The Http protocol is based on the TCP protocol. When the browser needs to obtain web page data from the server, it will issue an Http request.
Http will establish a connection channel to the server through TCP. When the data required for this request is completed, Http will immediately disconnect the TCP connection. The process is very short. Therefore, the HTTP connection is a short connection and a stateless connection. The so-called stateless means that every time the browser initiates a request to the server, it does not go through a connection, but establishes a new connection every time. If it is a connection, the server process can maintain the connection and remember some information status in the memory. After each request ends, the connection is closed and the relevant content is released, so no state is remembered and it becomes a stateless connection.
Recommended courses: PHP Tutorial.
As time goes by, the HTML page becomes more complex, and there may be many pictures embedded in it. At this time, it is inefficient to establish a TCP connection every time to access the picture. Therefore Keep-Alive was proposed to solve the problem of low efficiency. Starting from HTTP/1.1, Keep-Alive is enabled by default to keep the connection feature. Simply put, when a web page is opened, the TCP connection used to transmit HTTP data between the client and the server will not be closed. If the client When you visit the webpage on this server again, you will continue to use this established connection. Keep-Alive does not maintain the connection permanently. It has a retention time, which can be set in different server software (such as Apache).
Although the TCP connection is maintained for a period of time here, this time is limited and will still be closed at the time point, so we also regard it as closing after each connection is completed. Later, through Session, Cookie and other related technologies, the status of some users can also be maintained. But it still uses one connection every time, and it is still a stateless connection.
There used to be a concept that I couldn’t tolerate being confused about. That's why Http is a stateless short connection, while TCP is a stateful long connection? Isn't HTTP based on TCP? Why can it still be a short connection?
Now I understand that Http closes the TCP connection after each request is completed, so it is a short connection. When we use the TCP protocol directly through Socket programming, because we can control when to open and close the connection through the code area, as long as we do not close the connection through code, the connection will be in the process of the client and server. It always exists, and the relevant status data will always be saved.
HTTP is an object-oriented protocol belonging to the application layer. Due to its simple and fast method, it is suitable for distributed hypermedia information systems. It was proposed in 1990 and has been continuously improved and expanded after several years of use and development. The sixth version of HTTP/1.0 is currently used in the WWW. The standardization work of HTTP/1.1 is in progress, and the HTTP-NG (Next Generation of HTTP) proposal has been put forward.
The main features of the HTTP protocol can be summarized as follows:
1. Support client/server mode.
2. Simple and fast: When a client requests a service from the server, it only needs to transmit the request method and path. Commonly used request methods are GET, HEAD, and POST. Each method specifies a different type of contact between the client and the server. Due to the simplicity of the HTTP protocol, the program size of the HTTP server is small and the communication speed is very fast.
3. Flexible: HTTP allows the transmission of any type of data object. The type being transferred is marked by Content-Type.
4. No connection: The meaning of no connection is to limit each connection to only process one request. After the server processes the client's request and receives the client's response, it disconnects. This method saves transmission time.
5. Stateless: The HTTP protocol is a stateless protocol. Stateless means that the protocol has no memory ability for transaction processing. The lack of status means that if subsequent processing requires the previous information, it must be retransmitted, which may result in an increase in the amount of data transferred per connection. On the other hand, the server responds faster when it does not need previous information.
The above is the detailed content of The difference between http protocol and tcp protocol. For more information, please follow other related articles on the PHP Chinese website!