Understand HTTP, TCP, UDP, Socket, WebSocket in one article

Reprint: One article to understand HTTP, TCP, UDP, Socket, WebSocket-Technical Tutorial-Tunan Navigation

1. What is the difference between this bunch of keywords?

Where there is a name, there is a definition, and the premise of understanding has always been to understand the definition. It’s like asking you to describe the definition of a trapezoid now. If you don’t know the definition, you may treat the parallelogram as a special trapezoid, and you may confuse TCP and Socket, and you will even entangle TCP connection and Socket connection and HTTP What is the difference between the long/short connection.

In fact, it is easy to understand the difference. But before that, we must first clarify a concept, that is, the OSI seven-layer model, and the so-called five-layer model. What the hell is the four-layer model?

The OSI seven-layer model is an ideal standard proposed by the omnipotent International Organization for Standardization (ISO) to try to interconnect various computers around the world. To put it bluntly, the gap between ideal and reality is the gap between the seven-layer model and the five-layer model. The specific classification is as follows:

seven layer model

five layer model

four layer model

application layer

presentation layer

application layer

application layer

session layer

transport layer

transport layer

transport layer

Network layer

Network layer

Network layer

data link layer

data link layer

link layer/entity layer

physical layer

physical layer

The upper three layers of the seven-layer model are classified as the application layer, which is the TCP/IP five-layer model, and the lower two layers of the five-layer model are classified as the link layer or entity layer, which is the four-layer model.
That is to say, the so-called five or four layers can actually be regarded as unspoken rules formed for the convenience of understanding, and the specific implementation must still be based on the standard of the seven layers. After all, each layer has its own functions, and in order to complete the functions of each layer, everyone needs to abide by the relevant rules, that is, the agreement. Therefore, there is no need to pay too much attention to the layering of the model, whether it is five layers or four layers. For these invisible and intangible things, you only need to know that the Internet is layered, and there are only a few layers when they come and go. enough.

So, back to the first question, what is the difference between this bunch of keywords?

  • In essence, HTTP, WebSocket, TCP, UDP, and IP are all protocols, while TCP/IP is a combination of different protocols. You can also call it a protocol stack, protocol family, TCP/IP model, etc. You can Just be happy, anyway, they are nothingness and inedible things, and they are all unified rules formulated to complete the corresponding functions.
    And Socket (socket) is the real thing that can be operated. The essence of Socket is API, which is the abstraction or encapsulation of the TCP/IP protocol family by the ancestors. It is like a facade, giving you an entry to operate the TCP/IP protocol to establish a Socket connection. It is worth mentioning that this Socket refers to the Socket under network programming, not the Socket in Unix. Although the concept is similar, the Socket in Unix is ​​not based on these messy protocols, but on the file system of the operating system itself.

  • In terms of layering, HTTP and WebSocket are application layer protocols, TCP and UDP are transport layer protocols, and IP is a network layer protocol.

Second, what is the specific relationship between these keywords?

1. TCP and UDP

TCP is a connection-oriented transmission control protocol. After the TCP connection, the client and the server can send and receive messages to each other. Before the client or the server actively disconnects, the connection always exists, so it is called a long connection. Features: Time-consuming connection, no size limit for data transmission, accurate and reliable, first-served, first-served.
UDP is a connectionless user datagram protocol. The so-called connectionless means that there is no need to exchange information before transmitting data. There is no process of handshaking to establish a connection. It only needs to directly send the corresponding data to the specified address and port. Therefore, UDP is characterized by instability, fast speed, and broadcastability. Generally, data packets are limited to 64KB, and the first send may not necessarily come first.

2.HTTP

HTTP is an application based on the TCP protocol. When requesting, a TCP connection needs to be established, and the request packet needs to contain information such as the request method, URI, and protocol version. After the request is completed, the connection is disconnected to complete a request/response operation. Therefore, it is called a short connection.
The long connection maintained by keep-alive in HTTP/1.1 is to optimize the trouble and resource overhead of the three-way handshake of the TCP connection in each HTTP request. Only one TCP connection is established, and the request/response is completed on this channel multiple times. operate.
It is worth mentioning that the server cannot actively push messages to the client.

3.WebSocket

WebSocket is also a protocol, and it is also based on the TCP protocol. The specific process is that WebSocket first sends a request marked Upgrade through HTTP, and the server starts to establish a TCP connection after parsing, which saves the redundancy of uploading headers for each request of HTTP long connections. It can be understood that WebSocket is an optimization of HTTP. But WebSocket is not only supported on web applications.

4.Socket connection and TCP connection

In fact, this is just a word game. Establishing a Socket connection requires at least one pair of Sockets (sockets), and creating a Socket connection can specify different transport layer protocols, that is, TCP or UDP, so when using TCP to establish a connection, the Socket connection It is regarded as a TCP connection. UDP is connectionless.

5.Socket and WebSocket

Although these two have similar names, they are two completely different concepts, just like Java and JavaScript have nothing to do with each other. Socket is an interface encapsulated by a set of protocols for establishing Socket connections. Although WebSocket is a product of Html5, it is not limited to browser applications. Many languages ​​provide support for WebSocket, such as C, C++, Python, etc.

6. The relationship between HTTP, WebSocket and TCP

The HTTP communication process belongs to the method of "you push, I go". If the client does not send a request, the server will never be able to send data to the client. After the first HTTP request, WebSocket uses TCP channels for two-way communication. communication. Therefore, although both HTTP and WebSocket are based on the TCP protocol, they are two completely different communication methods.

Guess you like

Origin blog.csdn.net/beenles/article/details/123503890