The connection and difference between the communication protocols Http, Socket and WebSocket

Network protocol related video analysis:

From websocket protocol to tcp custom protocol, tcp sub-packet and sticky packet, clear text transmission to
realize the network protocol stack by hand, please prepare the environment and write code together
. The UDP technical points that must be asked in the server development job interview

Difference between WebSocket and Socket

Think of WebSocket as HTTP (application layer). What is the relationship between HTTP and Socket, and what is the relationship between WebSocket and Socket.

The HTTP protocol has a flaw: communication can only be initiated by the client, and the server cannot actively push information to the client.

The biggest feature of the WebSocket protocol is that the server can actively push information to the client, and the client can also actively send information to the server. It is a true two-way equal dialogue, which is a kind of server push technology.

The difference between socket and http:

Http protocol: a simple object access protocol, corresponding to the application layer. Http protocol is based on TCP link.
TCP protocol: Corresponding to the transport layer
ip protocol: Corresponding to the network layer
TCP/IP is a transport layer protocol, which mainly solves how data is transmitted in the network; and Http is an application layer protocol, which mainly solves how to package data.

Socket is an encapsulation of the TCP/IP protocol. Socket itself is not a protocol, but a call interface (API). Only through Socket can we use the TCP/IP protocol.

Http connection: http connection is the so-called short connection, and the client sends a request to the server, the connection will be disconnected after the server responds.

Socket connection: the so-called long connection of socket connection in time. In theory, once the connection between the client and the server is established, they will not be actively disconnected; however, due to various environmental factors, the connection may be disconnected, for example: server or client If the host is down, the network is faulty, or there is no data transmission between the two for a long time, the network firewall may disconnect the link and release network resources. Therefore, when there is no data transmission in a socket connection, a heartbeat message needs to be sent for a continuous connection. The specific heartbeat message format is defined by the developer.

1. HTTP long connection generally only lasts for one minute, and it is determined by the browser. It is difficult for your page to control this behavior.
Socket connection can be maintained for a long time, several days or months, as long as the network is continuous, the program does not end, and it can be programmed and controlled flexibly.
2. The HTTP connection is established on the Socket connection. In the actual network stack, the Socket connection is indeed a part of the HTTP connection. But from the perspective of HTTP protocol, its connection generally refers to that part of itself.

The TCP/IP protocol stack is mainly divided into four layers: application layer, transport layer, network layer, and data link layer.

Each layer has a corresponding protocol, as shown below
Insert picture description here

IP:

Network layer protocol; (Highway)

TCP and UDP:

Transport layer protocol; (truck)

HTTP:

Application layer protocol; (goods). HTTP (Hypertext Transfer Protocol) is a protocol that uses TCP to transfer information between two computers (usually a Web server and a client). The client uses a Web browser to initiate an HTTP request to the Web server, and the Web server sends the requested information to the client.

SOCKET:

Socket, TCP/IP network API. (Port wharf/station) Socket is the middleware abstraction layer for communication between the application layer and the TCP/IP protocol suite. It is a set of interfaces. Socket is an abstraction layer between the application layer and the transport layer. It abstracts the complex operations of the TCP/IP layer into a few simple interfaces. The application layer calls the implemented processes to communicate in the network.

TCP/IP:

It stands for Transmission Control Protocol/Internet Protocol and refers to a series of protocols. The TCP/IP model is simplified on the basis of the OSI model and becomes four layers, from bottom to top: network interface layer, network layer, and transmission Layer, application layer. The comparison with OSI architecture is as follows:

Insert picture description here

[Article benefits] C/C++ Linux server architect learning materials plus group 812855908 (data including C/C++, Linux, golang technology, Nginx, ZeroMQ, MySQL, Redis, fastdfs, MongoDB, ZK, streaming media, CDN, P2P, K8S, Docker, TCP/IP, coroutine, DPDK, ffmpeg, etc.)
Insert picture description here

TCP/UDP difference:

TCP

(Transmission Control Protocol): (similar to making a call)

Connection-oriented, reliable transmission (guarantee data correctness), orderly (guarantee data sequence), transmission of large amounts of data (streaming mode), slow speed, multiple requirements for system resources, complex program structure,

Each TCP connection can only be point-to-point,

The TCP header overhead is 20 bytes.

UDP

(User Data Protocol): (similar to texting)

It is oriented to non-connection, unreliable transmission (possible packet loss), disorder, transmission of a small amount of data (datagram mode), fast speed, less requirement for system resources, simple program structure,

UDP supports one-to-one, one-to-many, many-to-one and many-to-many interactive communication,

The header overhead of UDP is small, only 8 bytes.

TCP three-way handshake to establish a connection:

The first handshake: the client sends a syn packet (seq=x) to the server, and enters the SYN_SEND state, waiting for the server to confirm;

The second handshake: the server receives the syn packet, it must confirm the client's SYN (ack=x+1), and at the same time send a SYN packet (seq=y), that is, the SYN+ACK packet, and the server enters the SYN_RECV state;

The third handshake: The client receives the SYN+ACK packet from the server and sends an acknowledgment packet ACK (ack=y+1) to the server. After this packet is sent, the client and server enter the ESTABLISHED state and complete the three-way handshake.

The packet transmitted during the handshake process does not contain data. After the three-way handshake is completed, the client and server formally begin to transmit data. Ideally, once the TCP connection is established, the TCP connection will be maintained until either of the two parties actively closes the connection.

Host A sends a connection request packet to host B: "I want to send you data, can I?" This is the first conversation;

Host B sends to host A a data packet agreeing to connect and requesting synchronization (synchronization means that one of the two hosts is sending, the other is receiving and coordinating work): "Yes, when will you send it?" This is the second conversation;

Host A sends another data packet to confirm host B's request for synchronization: "I will send it now, you can continue!" This is the third conversation.

The purpose of the three "dialogues" is to synchronize the sending and receiving of data packets. After three "dialogues", host A officially sends data to host B.

Websocket

The Websocket protocol solves the problem of full-duplex communication between the server and the client.

Note: What is simplex, half-duplex, and full-duplex communication?

Information can only be transmitted in one direction as simplex;

Information that can be transmitted in both directions but not at the same time is called half-duplex;

Information that can be transmitted in both directions at the same time is called full duplex.

Websocket protocol analysis

The wensocket protocol consists of two parts: one is "handshake" and the other is "data transmission".

To understand http and sockets, you must first be familiar with the seven layers of the network: the physical data network transmission meeting table response, as shown in Figure 1

Insert picture description here

Figure 1

HTTP protocol: Hypertext transfer protocol, corresponding to the application layer, used to encapsulate data.

TCP/UDP protocol: Transmission control protocol, corresponding to the transport layer, mainly to solve the transmission of data in the network.

IP protocol: corresponding to the network layer, it also solves the transmission of data in the network.

When transmitting data, only the TCP/IP protocol (transport layer) is used. If there is no application layer to identify the content of the data, the transmitted protocol is useless.

There are many application layer protocols such as FTP, HTTP, TELNET, etc. You can define application layer protocols yourself.

The web uses HTTP as the transport layer protocol to encapsulate HTTP text information, and then uses TCP/IP as the transport layer protocol to send data to the network.

1. HTTP protocol

http is a short connection: the client sends a request to the server to send back a response. After the request is over, the link is actively released, so it is a short connection. The usual practice is to not need any data, but also to keep sending "keep connection" requests to the server at regular intervals. This ensures that the client is "online" on the server.

The HTTP connection uses the "request-response" method. Not only the connection is established when the request is made, but the server returns the data after the client requests the server.

2. Socket connection

To understand Socket, you must understand TCP connection.

TCP three-way handshake: No data is transmitted during the handshake process. After the handshake, the server and the client begin to transmit data. Ideally, once the TCP connection is established, the TCP connection will remain until either of the two parties actively disconnects. Go down.

Socket is an encapsulation of the TCP/IP protocol. Socket is just an interface, not a protocol. We can use the TCP/IP protocol through Socket. In addition to TCP, UDP protocol can also be used to transmit data.

When creating a Socket connection, you can specify the transport layer protocol, which can be TCP or UDP. When a TCP connection is used, the Socket is a TCP connection, and vice versa.

Socket principle

Socket connection requires at least a pair of sockets, divided into clientSocket, and serverSocket connection is divided into 3 steps:

(1) Server monitoring: The server does not locate the socket of a specific client, but is always in a monitoring state;

(2) Client request: The client's socket should describe the socket of the server to which it is connected, provide the address and port number, and then make a connection request to the server socket;

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

Socket is a long connection: Normally, the Socket connection is a TCP connection, so once the Socket connection is established, the two communicating parties start to send data to each other until the two parties disconnect. In practical applications, because there are too many network nodes, they will be disconnected during the transmission process. Therefore, the high-speed network must be polled and the node is active.

In many cases, it is necessary for the server to actively push data to the client to maintain real-time synchronization between the client and the server.

If both parties are connected by Socket, the server can send data directly to the client.

If the two parties are connected via HTTP, the server needs to wait for the client to send the request before sending the data back to the client.

Therefore, the client periodically sends a request to the server, which can not only stay online, but also ask the server if there is new data, and if so, it will send the data to the client.

Guess you like

Origin blog.csdn.net/qq_40989769/article/details/112673929