The relationship and difference between HTTP, TCP and UDP, socket and websocket

Time:2021-8-1

TCP / IP protocol stack is mainly divided into four layers: application layer, transmission layer, network layer and data link layer,
Each layer has corresponding protocols, as shown in the figure below
The relationship and difference between HTTP, TCP and UDP, socket and websocket

Conceptual understanding:

  • IP: network layer protocol( (Expressway)
  • TCP and UDP: transport layer protocol( Truck)
  • TCP/IP: represents transmission control protocol / Internet protocol, which refers to a series of protocols. TCP / IP model is simplified on the basis of OSI model and becomes four layers, from bottom to top: network interface layer, network layer, transport layer and application layer.
  • SOCKET: socket, API of TCP / IP network( Port (wharf / station) socket is an intermediate software abstraction layer for communication between application layer and TCP / IP protocol family. It is a group of interfaces. Socket is an abstract layer between application layer and transport layer. It abstracts the complex operation of TCP / IP layer into several simple interfaces. The supply layer calls the implemented processes to communicate in the network.
  • HTTP: application layer protocol( Goods). HTTP (Hypertext Transfer Protocol) is a protocol that uses TCP to transfer information between two computers (usually web server and client). The client uses the web browser to initiate an HTTP request to the web server, and the web server sends the requested information to the client.
  • WebSocket: websocket protocol is a new protocol of HTML5. It realizes full duplex communication between browser and server. The initial handshake needs to be completed with the help of HTTP request.
    Websokcet purpose: instant messaging, instead of polling.
    Instant messaging on websites is very common, such as web QQ, chat system, etc. According to the previous technical capability, it is usually solved by polling and comet technology.
    HTTP protocol is a non persistent, one-way network protocol. After establishing a connection, the server can only return the corresponding data after the browser sends a request to the server. When instant messaging is required, the browser sends a request to the server through polling at a specific time interval (such as 1 second), and then returns the latest data to the browser. The most obvious disadvantage of this method is that it needs to send requests continuously, and usually the header of HTTP request is very long. In order to transmit a small data, it needs to pay a huge price, which is very uneconomical and occupies a lot of broadband.
    Disadvantages: it will lead to too many unnecessary requests and waste traffic and server resources. Each request and response wastes a certain amount of traffic on the same header information.
    However, the emergence of websocket can make up for this shortcoming. In websocket, only the server and browser need to shake hands through HTTP protocol, and then establish a separate TCP communication channel for data transmission.

Note: what are simplex, half duplex and full duplex communication? Information can only be transmitted in one direction as simplex; Information can be transmitted in both directions, but cannot be transmitted in both directions at the same time, which is called half duplex;
When information can be transmitted in both directions at the same time, it is called full duplex.

Conceptual connection and difference:

  • TCP and UDP:_ TCP_, Transmission control protocol: (similar to calling) connection oriented, reliable transmission (ensuring data correctness), orderly (ensuring data order), transmitting a large amount of data (stream mode), slow speed, many requirements for system resources and complex program structure. Each TCP connection can only be point-to-point, and the TCP header overhead is 20 bytes_ UDP_,( User data protocol (similar to SMS): (non connected, unreliable transmission (possible packet loss), disorderly, transmitting a small amount of data (datagram mode), fast speed, less requirements for system resources, simple program structure, UDP supports one-to-one, one-to-many, many-to-one and many-to-many interactive communication, and the header overhead of UDP is small, Only 8 bytes.

TCP three times handshake to establish connection:The relationship and difference between HTTP, TCP and UDP, socket and websocket
First handshake: the client sends a syn packet (SEQ = x) to the server and enters syn_ Send status, waiting for server confirmation;
The second handshake: when the server receives the syn packet, it must confirm the syn (ACK = x + 1) of the customer, and also send a syn packet (SEQ = y), that is, syn + ACK packet. At this time, the server enters syn_ Recv status;
The third Handshake: the client receives the syn + ACK packet from the server and sends a confirmation packet ack (ACK = y + 1) to the server. After the packet is sent, the client and the server enter the established state and complete the three handshakes.
The packet transmitted during the handshake does not contain data. After three handshakes, the client and server officially start transmitting data. In an ideal state, once a TCP connection is established, TCP will terminate before either of the two sides of the communication actively closes the connection
The connection will always be maintained. Host a sends a connection request packet to host B: “can I send you data?”, This is the first dialogue;
Host B sends a packet to host a agreeing to connect and requiring synchronization (synchronization means that one host is sending and the other is receiving and coordinating work): “yes, when will you send it?”, This is the second dialogue;
Host a sends another packet to confirm that host B requests synchronization: “I’ll send it now, you go on!”, This is the third dialogue.
The purpose of three “conversations” is to synchronize the sending and receiving of data packets. After three “conversations”, host a officially sends data to host B.

  • HTTP and websocket

Similarities: 1. They are all based on TCP and reliable transmission protocols. 2. Both are application layer protocols.
Differences: 1. Websocket is a two-way communication protocol, which simulates the socket protocol and can send or receive information in both directions. 2. HTTP is one-way. 2. Websocket needs handshake to establish connection.
Contact: when websocket establishes a handshake, the data is transmitted through HTTP. However, after establishment, the HTTP protocol is not required for real transmission.

Traditional HTTP request response client server interaction diagram:
The relationship and difference between HTTP, TCP and UDP, socket and websocket
Websocket request response client server interaction diagram:
The relationship and difference between HTTP, TCP and UDP, socket and websocket
As can be seen from the comparison of the above figure, compared with the traditional HTTP mode in which each request response requires a connection between the client and the server, websocket is a TCP long connection communication mode similar to socket. Once the websocket connection is established, the subsequent data is transmitted in the form of frame sequence. Before the client disconnects the websocket connection or the server disconnects, the client and the server do not need to re initiate the connection request. In the case of massive concurrency and large interactive load traffic between the client and the server, it greatly saves the consumption of network bandwidth resources and has obvious performance advantages. Moreover, the client sends and receives messages on the same persistent connection, which has obvious real-time advantages.

Let’s look at the difference between websocket communication and traditional HTTP through the interactive messages between the client and the server:
On the client side, new websocket instantiates a new websocket client object and connects to the websocket URL of the server side similar to WS: / / yourdomain: port / path. The websocket client object will automatically resolve and recognize it as a websocket request, so as to connect to the server port and execute the handshake process of both sides. The format of data sent by the client side is similar:
1. Websocket client connection message:

`GET /webfin/websocket/ HTTP/1.1
Host: localhost
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: xqBt3ImNzJbYqRINxEFlkg==
Origin: 
http://localhost
:8080
Sec-WebSocket-Version: 13`

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9

It can be seen that the websocket connection message initiated by the client is similar to the traditional HTTP message. “Upgrade: websocket” parameter value indicates that this is a websocket type request, “sec websocket key” is a Base64 encoded ciphertext sent by the websocket client, which requires the server to return a corresponding encrypted “sec websocket accept” response, Otherwise, the client will throw an “error during websocket handshake” error and close the connection.

2. Websocket server response message:

`HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: K7DJLdLooIwIG/MOpvWFB3y3FE8=`

*   1
*   2
*   3
*   4

The value of “sec websocket accept” is returned to the client after the server uses the same key as the client. “Http / 1.1 101 switching protocols” means that the server accepts the client connection of websocket protocol. After such request response processing, the websocket connection handshake of the client and the server is successful, and then TCP communication can be carried out.

In terms of development, the websocket API is also very simple. We only need to instantiate the websocket, create a connection, and then the server and client can send and respond to each other.

  • Websocket and socket

Socket is the transport control layer interface, and websocket is the application layer protocol.

You can think of websocket as HTTP (application layer). What is the relationship between HTTP and socket? What is the relationship between websocket and socket.
When two hosts communicate, they must be connected through socket, and socket uses TCP / IP protocol to establish TCP connection. TCP connection depends more on the underlying IP protocol, and the connection of IP protocol depends on lower levels such as link layer.
Websocket, like HTTP, is a typical application layer protocol.
The relationship and difference between HTTP, TCP and UDP, socket and websocket