IOS HTTP protocol and TCP protocol simple understanding

Source: Internet
Author: User

 

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


The TCP protocol corresponds to the transport layer, and the HTTP protocol corresponds to the application layer, which is inherently not comparable. The HTTP protocol is based on the TCP protocol, which sends an HTTP request when the browser needs to fetch the Web page data from the server. HTTP will establish a connection channel to the server via TCP, and when the data required for this request is complete, HTTP will immediately disconnect the TCP connection, which is a very short process. So an HTTP connection is a short connection, a stateless connection. The so-called stateless, refers to the browser every time the request to the server, not through a connection, but each time to establish a new connection. If it is a connection, the server process can hold the connection and remember some information status in memory. And each time the request ends, the connection is closed, the related content is released, so you can not remember any state to become a stateless connection.

Over time, HTML pages have become more complex, with many images embedded in them, and it is inefficient to create a TCP connection every time a picture is accessed. Therefore, Keep-alive is proposed to solve the problem of low efficiency. From http/1.1 onwards, the default is to open the keep-alive, to maintain the connection characteristics, in short, when a Web page opens, the client and server for the transmission of HTTP data between the TCP connection will not be closed, if the client again access to the Web page on this server, will continue to use this established connection keep-alive does not permanently maintain the connection, it has a hold time and can be set in different server software (such as Apache) this time. Although the TCP connection is maintained here for a period of time, the time is limited and the time is still closed, so we consider it to be closed each time the connection is completed. Later, through the session, cookies and other related technologies, can also maintain some user status. However, each time a connection is used, the connection remains stateless.

There used to be a concept that was too tolerant to be confused. That is why HTTP is a stateless short 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 is clear that HTTP is shutting down the TCP connection after each request is completed, so it is a short connection. When we use the TCP protocol directly through the socket programming, we can control when the connection is closed by the code area, as long as we do not close the connection through the code, the connection will persist in the process of the client and server, and the related state data will be kept.

In C # There will be sockets, in fact the socket is the TCP/IP protocol encapsulation, the socket itself is not a protocol, but a calling interface (API). The advent of sockets only makes it easier for programmers to use the TCP/IP protocol stack, which is an abstraction of the TCP/IP protocol, thus forming some of the most basic function interfaces we know, such as Create, listen, connect, accept, send, Read and write, and so on.

Description of the comparison image: HTTP is a sedan that provides a specific form of encapsulation or display data; The socket is the engine that provides the ability to communicate over the network. In terms of programming from C #, for convenience, you can directly choose which car HTTP has been made to interact with the server. But sometimes, because of environmental factors or some other custom requests, you have to use the TCP protocol, then you need to use socket programming, and then handle the data you get. It's like you're building a truck with your existing engine and going 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 TCP connection with the server first. Once the connection is established, the browser process and the server process can access TCP through their sockets. As mentioned earlier, the client socket is the "gate" between the client process and the TCP connection, and the server-side socket is the "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 into its own socket, the message is completely under TCP control. TCP provides a reliable data transfer service to HTTP, which means that each HTTP request message sent by the customer will eventually reach the server without compromise, and each HTTP response message sent by the server will eventually reach the customer without damage.

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, but long-connected. Transferring large files is faster. Need to look at specific scenarios. On the server side, if the program is to take a long connection, then you can control the number of connections to the server at the same time, to prevent multiple connections at the same time. However, with a short connection, it is not possible to control the number of connections to the server at the same time, which is also an advantage to handle a large number of connection requests at the same time. However, if the amount of connection requests is too large, it may cause the server to stop working.

WebService does not require a connection, it can support at least tens of 100,000 of requests in a second, each request is then released, and there is no spare memory consumption. The number of simultaneous connections is generally not limited, which is an advantage. The Message queue needs to establish a connection, and it's hard to support thousands of connections. Because each connection consumes a certain amount of space storage in memory, even if the data is not being requested. restrictions, such as SQL Server database servers, typically connect up to 16 connections at a time.

The HTTP protocol must pass through the specified port, 80, so the general computer does not restrict this port, so the HTTP protocol can go smoothly through the firewalls on all the machines. With the socket programming, you need to specify a specific port, it is likely that the port is disabled in an environment, then can not penetrate the firewall. IIS is using port 80, which is the program that is listening to this 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 to the URL on the server is sent to the website program via port 80. The client browser is then sent through this port.

IOS HTTP protocol and TCP protocol simple understanding

Related Article

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.