1. Layer 7 protocols

The application layer

The presentation layer

The session layer

The transport layer

The network layer

Data link layer

The physical layer

2.WebSocket is different from Socket

WebSocket can be thought of as HTTP(application layer), HTTP and Socket is what the relationship between WebSocket and Socket is.

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

The biggest feature of WebSocket protocol is that the server can take the initiative to push information to the client, and the client can also take the initiative to send information to the server. It is a real two-way equal dialogue and belongs to a server push technology

3. The difference between Socket and Http:

Http: a simple object access protocol corresponding to the application layer. Http protocol is based on TCP links;

Tcp protocol: corresponding to the transport layer;

Ip protocol: corresponding to the network layer;

TCP/IP is a transport layer protocol that mainly solves how data is transmitted on the network. Http is an application-layer protocol that deals with how to wrap data.

The Socket is the encapsulation of TCP/IP. The Socket itself is not a protocol, but an API. Through the Socket, we can use TCP/IP.

Http connection: An Http connection is a short connection in which the client sends a request to the server and the connection is broken after the server responds.

Socket connection: A socket connection or a so-called long connection. Theoretically, once a connection is established between the client and server, the connection will not be actively broken. However, the connection may be disconnected due to various environmental factors, for example, the server or client host is down, the network is faulty, or there is no data transmission between the two hosts for a long time. The network firewall may disconnect the connection to release network resources. Therefore, when there is no data transmission in a socket connection, heartbeat messages need to be sent for sequential connections. The specific heartbeat message format is defined by the developer himself.

1.HTTP long connections typically last only a minute, and are browser-determined, so it’s hard for your page to control this behavior. The Socket connection can last for a long time, days or months, as long as the network is continuous, the program does not end, and it is programmable and flexible.

2.HTTP connections are built on Socket connections. In a real network stack, Socket connections are indeed part of HTTP connections. But in terms of the HTTP protocol, its connection generally refers to its own part.

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

Each layer has its own protocol, as shown below

HTTP:

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

TCP and UDP:

Transport layer protocol; (trucks)

The SOCKET:

Socket, TCP/IP network API. Socket is an intermediate software abstraction layer for the communication between the application layer and the TCP/IP protocol family. It is a group of interfaces. Socket is an abstraction layer between the application layer and the transport layer. It abstracts the complex operation of TCP/IP layer into several simple interfaces for the application layer to call the realized process to communicate in the network.

TCP/IP:

TCP/IP stands for Transmission Control Protocol/Internet Protocol, which refers to a series of protocols. The TCP/IP model is simplified from the OSI model into four layers, from top to bottom: application layer, transport layer, network layer, and network interface layer. Comparisons with OSI architecture are as follows:

TCP/UDP difference:

TCP

Transmission Control Protocol (Transmission Control Protocol)

Connection-oriented, reliable transmission (ensure data correctness), orderly (ensure data order), large amount of data transmission (flow mode), slow speed, more requirements on system resources, complex program structure,

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

The TCP header costs 20 bytes.

UDP

(User Data Protocol) (Similar to SMS)

Non-connection oriented, unreliable transmission (packet loss), disorderly, small amount of data transmission (datagram mode), fast, less requirements on system resources, simple program structure,

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

The header of UDP has a small overhead of only 8 bytes.

TCP three-way handshake to establish a connection:

First handshake: The client sends a SYN packet (seq= X) to the server and enters the SYN_SEND state for confirmation.

Second handshake: After receiving a SYN packet, the server must acknowledge the client’s SYN (ACK = X +1) and send a SYN packet (SEq = Y). In this case, the server enters the SYN_RECV state.

Third handshake: After receiving the SYN+ACK packet from the server, the client sends an ACK packet (ACK = Y +1) to the server. After the packet is sent, the client and the server enter the ESTABLISHED state to complete the three-way handshake.

The packet transmitted during the handshake does not contain data. After three handshakes, the client and server start data transmission. Ideally, once a TCP connection is established, it is maintained until either of the communicating parties voluntarily closes the connection.

Host A sends A connection request packet to host B: “I want to send you data, ok?” This is the first dialogue;

Host B sends host A A packet agreeing to connect and asking for synchronization (synchronization is when two hosts are sending and one is receiving, coordinating work) : “Yes, when will you send?” This is the second dialogue;

Host A sends A data packet to confirm host B’s request synchronization: “I send now, you then!” This is the third dialogue.

The purpose of the triple chat is to synchronize the sending and receiving of data packets. After the triple chat, host A sends data to host B.

Websocket

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

Note: what is simplex, half duplex, full duplex communication?

Information can only be transmitted one-way as simplex;

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

When information can be sent both ways simultaneously, it is called full-duplex.

Websocket protocol parsing

The WENsocket protocol consists of two parts: handshake and data transmission.

To understand HTTP and sockets, familiarize yourself with layer 7:

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

TCP/UDP: a transmission control protocol corresponding to the transport layer, which transfers data over the network.

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

Only THE TCP/IP protocol (transport layer) is used to transfer data. Without the application layer to identify the data content, the protocol after transmission is useless.

Application-layer protocols Include FTP,HTTP, and TELNET. You can customize application-layer protocols.

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 the data to the network.

HTTP HTTP is a short connection: the client sends a request and the server sends a response. After the request ends, the link is actively released, so it is a short connection. In general, no data is required and “stay connected” requests are sent to the server at regular intervals. This ensures that the client is “online” on the server.

HTTP connections use a “request-response” approach. Not only is the connection established at request time, but the server does not return data until the client requests it.

Second, Socket connection

To understand sockets, you must understand TCP connections.

TCP three-way handshake: No data is transmitted during the handshake. Data is transmitted between the server and client only after the handshake. Ideally, once a TCP connection is established, the TCP connection remains open until either of the two parties disconnects.

Socket is the encapsulation of TCP/IP. A Socket is just an interface, not a protocol. We can use TCP/IP through Socket.

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

Principle of the Socket

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

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

(2) Client request: the client socket should describe the socket of the server it wants to connect to, 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 from the client socket and establishes a new thread to send the description of the server socket to the client. Once the client confirms this description, the connection is formally established. The server socket remains listening and continues to receive connection requests from other client sockets.

A Socket is a long connection: Generally, a Socket connection is a TCP connection. Therefore, once a Socket connection is established, the communication parties begin to send data to each other until the connection is disconnected. In practical applications, because there are too many nodes in the network, they will be disconnected during transmission. Therefore, the node is in active state through polling high-speed network.

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

If the two parties are connected through sockets, the server can directly send data to the client.

If the connection is HTTP, the server can send data back to the client only after the client sends a request.

Therefore, the client periodically sends requests to the server, not only to stay online, but also to ask the server if there is new data, and if so, to pass the data to the client.