HTTP and protocol TCP protocols are easy to understand

Source: Internet
Author: User
Tags message queue

Write code in C #, many times you will encounter the HTTP protocol or TCP contract, here is a simple understanding.

TCP corresponds to the Transport Layer protocol, and HTTP corresponds to the application-layer protocol, in essence, there is no comparability between the two. HTTP this protocol is based on TCP, and when the browser needs to fetch the Web page data from the server, an HTTP request is made.

HTTP establishes a connection channel to the server over TCP. When the requested data is complete, HTTP disconnects the TCP connection immediately, which is a very short process. So an HTTP connection is a short connection. is a stateless connection. the so-called stateless, refers to the browser every time the request to the server. Not through a connection. Instead, each time a new connection is established.

Assuming it is a connection, the server process can hold the connection and remember some information state in memory. The connection is closed at the end of each request. The relevant content is released, so you can't remember whatever the state is. becomes a stateless connection.

Over time, HTML pages have become more complex. There may be a lot of images embedded in it, so it's inefficient to make a TCP connection every time you visit a picture.

Therefore, Keep-alive is proposed to solve the problem of low efficiency. From http/1.1 onwards. By default, the Keep-alive is turned on, keeping the connection feature, simply speaking, when a webpage is opened. The TCP connection between the client and server for transmitting HTTP data does not close, assuming that the client again visits the Web page on the server and continues to use the established connection keep-alive does not permanently remain connected. It has a hold time. The ability to set this time in different server software (such as Apache).

Although this is maintained over a period of time using a TCP connection. But this time is a limited range. It will still be closed at the point of time. So we also consider it to be closed each time the connection is complete. Later, through the session, cookies and other related technologies, can also maintain some user status. However, each time you use a connection, it is still a stateless connection.

There was a very tolerant notion that was not clear.

That is why HTTP is a short, stateless connection. And TCP is a stateful long connection? HTTP is not built on the basis of TCP, why can it be a short connection? now it's clear. HTTP is a short connection when the TCP connection is turned off after each request is completed. When we use the TCP protocol directly through socket programming, we are able to control when the connection is closed when we open it through the code area. Just because we do not close the connection through the code, the connection will persist in the client and server processes, and the related state data will be kept.

There will be sockets in C #. The socket is actually encapsulated in the TCP/IP protocol, and the socket itself is not a protocol. Instead, it is a calling interface (API). The advent of sockets is simply an abstraction of the TCP/IP protocol that makes it easier for a program ape to use the TCP/IP protocol stack. Thus forming some of the most important function interfaces we know. For example, create, listen, connect, accept, send, read, write, and so on.

A descriptive narrative of the comparative Image: HTTP is a sedan. Provides a detailed form of encapsulating or displaying data; The socket is the engine that provides the ability to communicate over the network. For the perspective of programming from C #. For convenience, you can directly choose which car HTTP has been made to interact with the server. However, sometimes due to environmental factors or some other customized requests, you will need to use the TCP protocol, then you need to use the socket programming, and then to process the obtained data.

It's like you built a truck with an existing engine. To interact with the server.


Both http/1.0 and http/1.1 use TCP as the underlying transport protocol.

The HTTP client initiates the establishment of a connection with the servertcp first.

Once the connection is established. Browser processes and server processes are able to access TCP through their own sockets. As mentioned earlier, the client socket is the "gate" between the client process and the TCP connection. A server-side socket is a "gate" between the server process and the same TCP connection.

The customer sends an HTTP request message to his socket and receives an HTTP response message from its own socket. Similarly, the server receives an HTTP request message from its own socket and sends an HTTP response message to its own socket. Once a client or server sends a message to its own socket. This message is completely under the control of TCP.

TCP provides a reliable transport data service to HTTP, which means that every HTTP request message sent by a customer will finally reach the server without compromise, and every HTTP response message sent by the server will eventually reach the customer without compromise.

C # code connects to a remote database using the TCP protocol. Each time a connection is new, Connection.Open opens the TCP connection. Connection. Close when the connection is closed. The underlying FTP is also TCP, which is just a long connection.

Transferring large files is faster.

Need to see detailed scenes. On the server side. Assuming the program is a long connection, you can control the number of connections to the server at the same time. Prevent multiple connections at the same time.

However, by taking a short connection, you cannot control the number of connections to the server at the same time. This is also a merit. Ability to handle large number of connection requests at the same time. However, assuming that the amount of connection requests is too large, the server may stop working.

WebService does not need to be connected, can support at least tens of 100,000 of requests in a second, each request is then released, no spare memory consumption. Generally does not limit the number of connections at the same time, which is an advantage.

The Message queue needs to establish a connection, and it is very hard to support thousands of connections. Because each connection does not request data, it consumes a certain amount of space storage in memory. Be limited. For example, SQL Server database server. Usually connect up to 16 at the same time.


The HTTP protocol must pass the specified port. 80, so the general computer does not restrict this port, sothe HTTP protocol can go through the firewall on all the machines smoothly.

If you use socket programming, you need to specify a specific port. Then it is very likely that the port is disabled in an environment, so it cannot penetrate the firewall. IIS uses 80port, which is the program that has been listening to the port. Once a connection has been found to this port, he will respond and then establish the connection. The connections here are all short connections.

So your request for the URL on the server is a live 80port program. Then this port sends the client browser.

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

HTTP and protocol TCP protocols are easy to understand

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.