The TCP/IP protocol stack is divided into four layers: application layer, transport layer, network layer, and data link layer. Each layer has corresponding protocols, as shown in the following figure

IP:

Network layer protocol; (Highway)

TCP and UDP:

Transport layer protocol; (trucks)

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.

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 set of protocols. The TCP/IP model is simplified from the OSI model into four layers, from bottom to top: network interface layer, network layer, transport layer, and application layer. Comparisons with OSI architecture are as follows:

TCP/UDP difference:

TCP

(Transmission Control Protocol) : (Similar to making a phone call) Connection-oriented, reliable transmission (ensuring data correctness), orderly (ensuring data sequence), large data transmission (flow mode), low speed, high requirements on system resources, complex program structure, each TCP connection can only be point-to-point, the cost of the TCP header is 20 bytes.

UDP

(User Data Protocol) : (like text) for the connection, the unreliable (it may be lost package), disorderly, transfer a small amount of data (datagram mode), fast speed, less to the requirement of system resources, the program structure is relatively simple, UDP support one-to-one, one-to-many, many-to-one and many-to-many interaction communication, UDP first overhead is small, only eight 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 resolution The WENsocket protocol consists of two parts: handshake and data transmission.

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.

WebSocket protocol was born in 2008 and became an international standard in 2011. All browsers already support it. Its biggest characteristic is that the server can take the initiative to push information to the client, the client can also take the initiative to send information to the server, is a real two-way equal dialogue, belongs to a server push technology.

Reference:

Remove the veil of Socket programming

Figure TCP-IP protocol

Android Advanced 12: Process Communication Socket (also review TCP UDP)

WebSocket and TCP/IP

Websocket history and usage details

WebSocket tutorial