Differences and similarities between Websocket protocol-http protocol-tcp protocol

form of communication

  • Simplex communication - data can only be transmitted in one direction, one party sends data, and the other party receives data

  • Half-duplex communication - data can be transmitted in both directions but not both directions at the same time

  • Full-duplex communication - data can be sent and received in both directions at the same time

Note: The communication mode of http is divided into versions

http1.0: simplex. Because it is a short connection, after the client initiates the request, the server will disconnect after processing the request and receiving the response from the client.

http1.1: half-duplex. Persistent connections are enabled by default keep-alive, and multiple requests can be sent with one connection enabled.

http2.0: Full duplex, allowing the server to actively send data to the client.

Long and short connections

Short connection - any 一次完整的消息交互(发请求-收响应)situation where the connection is immediately disconnected (one party sends a FIN message) is called a short connection

Long connection - An obvious feature of a long connection is that there will be heartbeat messages (there may be no heartbeat), and the general heartbeat interval is about 30S or 1MIN. You can see regular heartbeat message interactions with wireshark capture packets (there may be millisecond level of error).

1. websocket protocol long and short connection

websocket is a long connection - you only need to establish a connection once, and it can be maintained forever

2. http protocol long and short connection - version

In HTTP/1.0, short connections are used by default. That is to say, every time the browser and server perform an HTTP operation, a connection is established, and the connection is terminated when the task is completed.

HTTP1.1 maintains a long connection by default (HTTP persistent connection, also translated as persistent connection) to maintain connection characteristics. After the data transmission is completed, keep the TCP connection undisconnected (no RST packets, no four-way handshake), and wait to continue using this channel to transmit data under the same domain name;

3. Long and short connection of tcp protocol

TCP long connection - after the successful handshake at the TCP layer, the connection will not be disconnected immediately, and multiple messages (including heartbeats) will be exchanged on the basis of this connection until any party (client or server) actively disconnects the connection , this process is called a complete long connection.

TCP short connection - After receiving the response from the server, the client immediately sends a FIN message to actively release the connection. There are also cases where the server actively disconnects, which is the case where the connection is disconnected immediately after a message interaction (send request-receive response).

Protocol relationship table - both websocket and http are based on tcp, a reliable transmission protocol, and both are application layer protocols

Protocol introduction - attention to details

1. websocket protocol

  • WebSocket is a network communication protocol. RFC6455 defines its communication standard.

  • WebSocket is a new protocol under HTML5 (the websocket protocol is essentially a tcp-based protocol)

  • When WebSocket establishes the handshake, the data is transmitted through HTTP. But after the establishment, the HTTP protocol is not required for real transmission

  • Websocket stipulates a communication specification. Through a handshake mechanism, a TCP-like connection can be established between the client and the server, thereby facilitating the communication between them.

  • Before the emergence of websockets, web interactions were generally short or long connections based on the http protocol

  • websocket is a brand new protocol, not part of http stateless protocol, the protocol name is "ws"

  • It is said that it is TCP transmission, which is mainly reflected in the fact that after a long connection is established, the browser can send data to the server, and the server can also send requests to the browser. Of course, its data format is not defined by itself, but the outer layer of the data to be transmitted has an outer layer package stipulated by the ws protocol.

2. http protocol

  • HTTP is a stateless, connectionless, one-way application layer protocol. It adopts a request/response model. Communication requests can only be initiated by the client, and the server responds to the request. Such disadvantages are obviously very big. As long as the status of the server changes continuously, the client must respond in real time, polling through javascript and ajax, which is obviously very troublesome. At the same time, the efficiency of polling is low and it is very wasteful. Resources (http always open, repeated connections). ,

  • And http is a connection initiated by the browser to the server, and the server does not know this connection in advance.

3. tcp protocol

  • TCP (Transmission Control Protocol) is a connection-oriented protocol that provides reliable data transmission services. TCP establishes a logical connection before transmitting data, and disconnects after the transmission is complete. TCP also provides functions such as data flow control, congestion control , and error control.

  • TCP is a full-duplex communication protocol. This means that during the communication, both parties can send and receive data at the same time.

Protocol difference comparison chart

point of difference tcp http websocket
Long and short connection Long and short connection version Long connection
model layer transport layer application layer application layer
communication method full duplex communication version full duplex communication

Summarize:

After this process, I believe you have a preliminary deep impression on the differences and similarities between the Websocket protocol-http protocol-tcp protocol, but the situation we encounter in actual development must be different, so we need to understand it The principle is always the same. Come on, hit the workers!

Please point out any deficiencies, thank you -- Fengguowuhen

Guess you like

Origin blog.csdn.net/weixin_53579656/article/details/131945751