[Linux Network] The difference between TCP UDP socket HTTP webSocket

Table of contents

1. OSI & TCP/IP model

2. The relationship between several

3. HTTP

4. Socket

5. WebSocket

5.1. WebSocket advantages


1. OSI & TCP/IP model

First we need to understand the OSI seven-layer model and the corresponding TCP/IP four-layer model.

    As can be seen from the figure below, TCP UDP works at the transport layer, while HTTP WebSocket works at the application layer, but socket does not belong to any layer in the seven-layer model. It can be understood that socket works implicitly between the transport layer and the application layer. containing layer.

  Socket itself is not a protocol, but encapsulates the TCP/UDP protocol in the transport layer, hiding how the internal TCP/UDP is transmitted from the user, and only provides a set of interfaces (API) for programmers to call (socket words) to complete socket programming. Through the socket interface, we can use the TCP/UDP protocol.

2. The relationship between several

Application layer protocols   such as HTTP WebSocket call transport layer protocols such as TCP/UDP through the socket interface to achieve network communication.

  TCP/ UDP => Socket => HTTP WebSocket

  To sum up, our programming will not directly call TCP/UDP, but communicate through their encapsulated interface Socket. It can be said that almost all communication on the network now is completed through Socket at the bottom layer, and everything is Socket.

3. HTTP

HTTP is a hypertext transfer protocol   based on TCP protocol applications and belongs to the application layer protocol. A TCP connection needs to be established when requesting, and the connection is disconnected after the request is completed to complete a request/response operation.

  In the HTTP protocol, the client always initiates a request and the server sends back a response. This limits the use of the HTTP protocol and makes it impossible for the server to push messages to the client when the client does not initiate a request.

  The HTTP protocol is a stateless protocol, and there is no correspondence between this request and the previous request from the same client.

4. Socket

  Socket is an encapsulation of the TCP/IP protocol. Socket itself is not a protocol, but a calling interface (API).

  A Socket connection requires a pair of sockets, one running on the client and the other running on the server! The connection between them is divided into three steps: server monitoring , client request , and connection confirmation .

(1) Server monitoring: The server socket does not locate the specific client socket, but is in a state of waiting for connection and monitors the network status in real time.

(2) Client request: The connection request is made by the client's socket, and the target to be connected is the server's socket. Therefore, the client's socket must first describe the socket of the server it wants to connect to, indicate the address and port number of the server-side socket, and then make a connection request to the server-side socket.

(3) Connection confirmation: When the server-side socket listens or receives a connection request from the client socket, it responds to the client socket request, establishes a new thread, and connects the server-side socket. A description of the word is sent to the client. Once the client confirms this description, the connection is established. The server-side socket continues to be in the listening state and continues to receive connection requests from other client sockets.

5. WebSocket

  WebSocket is also a protocol and is also based on the TCP protocol. It can be understood that WebSocket is an optimization of HTTP, but WebSocket is not only supported on web applications. 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.   

  In order to establish a WebSocket connection, the client browser must first initiate an HTTP request to the server. This request is different from the usual HTTP request and contains some additional header information . This additional header information is used to complete the handshake process and upgrade the protocol. the process of. As shown below:

5.1. WebSocket advantages

  1. Save bandwidth. This method of continuously polling the server data uses the http protocol. The head information is very large and the proportion of valid data is low. However, using the WebSocket method, the header information is very small and the proportion of valid data is high.
  2. No waste. The polling method may poll 10 times before encountering the server data update, so the first 9 polls are in vain because the changed data is not obtained. WebSocket is actively sent back by the server, and all data coming is new.
  3. Real-time performance, considering the server pressure, it is impossible to use polling method for a short time interval, otherwise the server pressure will be too much, so the polling time interval is relatively long, several seconds, set to more than ten seconds. WebSocket is actively pushed by the server, and its real-time performance is the highest.
     

If there are any shortcomings in this article, you are welcome to comment below and I will correct it as soon as possible.

Guess you like

Origin blog.csdn.net/m0_63198468/article/details/132459471