Linux network programming | TCP/IP protocol overview

TCP/IP protocol overview

1. The layered model of TCP/IP

The 4-layer model of the
Insert picture description here
TCP/IP protocol is simplified on the basis of the OSI 7-layer protocol model. The TCP/IP protocol is a complex protocol composed of a set of specialized protocols, which include a number of sub-protocols. The following describes the role of each layer in the overall TCP/IP architecture

  • Network interface layer: It is the bottom layer of TCP/IP protocol software, responsible for converting binary streams into data frames, and sending and receiving data frames. Data frame is the basic unit of network transmission
  • Network layer: Responsible for providing basic data packet transmission functions, so that each data packet can reach the destination host
  • Transport layer: responsible for providing communication services between applications (also known as end-to-end communication)
  • Application layer: the layer of communication between applications

2. TCP/IP protocol communication model

TCP/IP protocol communication protocol model
Insert picture description here
Insert picture description here
TCP/IP communication protocol structure
Insert picture description here
Data encapsulation and transmission process
Insert picture description here

3. TCP/IP core protocol

The core protocols in the TCP/IP protocol group are designed to run on the network layer and the transport layer. They provide communication services for each host in the network and also provide services for the protocol at the highest layer (application layer) of the model. The following mainly introduces the transport layer TCP and UDP protocols

3.1 TCP

The upper layer of TCP is the application layer, and TCP provides reliable object-oriented data stream transmission services to the application layer. TCP data transmission realizes the data transfer from one application to another. It provides high-reliability communication (that is, communication with no errors, no data loss, no data out of order, and no repeated arrival of data). The application program implements data communication at the application layer by submitting the address and port number of the data sending/receiving end to the TCP layer. The source/destination of IP can uniquely distinguish the connection of two devices in the network, and the source/destination of socket can uniquely distinguish the connection of two applications in the network.
TCP is connection-oriented, that is, when both computers communicate Establish a connection, then perform data communication, and finally remove the connection. When TCP establishes a connection, it is divided into three steps (ie, 3-way handshake):

  • Step 1: (A->B) Host A sends a TCP message containing SYN, which is the synchronization flag, to host B. The SYN synchronization message will indicate the port used by the client and the initial sequence number of the TCP connection
  • Step 2: (B->A) After receiving the SYN message from the client, host B will return a SYN+ACK message, indicating that the request of host B is accepted, and the TCP sequence number is increased by 1, ACK is confirmation
  • Step 3: (A->B) Host A also returns an acknowledgment message ACK to the server. Similarly, the TCP sequence number is increased by 1, and a TCP connection is completed.
    Insert picture description here

The structure of the TCP data header is shown below

  • Source port, destination port: 16 bits, identify the remote and local ports
  • Sequence number: 32 bits, identifying the sequence of the datagrams sent
  • Confirmation number: 32 bits, the sequence number of the next datagram that you want to receive
  • TCP header length: 4 bits, indicating how many 32-bit words are contained in the TCP header
  • 6 unused
  • ACK: Set to 1 to indicate that the confirmation number is legal; if it is 0, then the datagram does not contain confirmation information and the confirmation field is omitted
  • PSH: indicates the data with the PUSH flag, so the receiver requests that the datagram can be sent to the application as soon as it arrives, instead of waiting until the buffer is full before sending
  • RST: used to reset the wrong connection due to host crash or other reasons, and can also be used to reject illegal datagrams or reject connection requests
  • SYN: used to establish a connection
  • FIN: used to release the connection
  • Window size: 16 bits, the window size field indicates how many bytes can be sent after the confirmation byte
  • Checksum: 16 bits, set to ensure high reliability, is the sum of the check header, data and pseudo TCP header
  • Options: 0 or more 32-bit words, including options such as maximum TCP load, window ratio, and selection of retransmission datagrams

Insert picture description here

3.2 UDP

UDP stands for User Datagram Protocol, which is an unreliable transmission protocol oriented to connectionless. It does not require a 3-way handshake to establish a connection. At the same time, a UDP application can be used as the client or server
UDP data packet header structure of the application at the same time

  • Source address, destination address: 16 bits, identifying the remote and local port numbers
  • The length of a data packet refers to the total number of bytes including the header and data part. Because the length of the packet header is fixed, this field is mainly used to calculate the variable-length data part (also known as data load)

Insert picture description here

3.3 Choice of protocol

The selection of the protocol should take into account the reliability of the data, the real-time nature of the application and the reliability of the network

  • Applications that require high data reliability need to choose TCP protocol, and applications that require less data reliability can choose UDP transmission
  • The three-way handshake and retransmission confirmation in the TCP protocol can ensure the reliability of data transmission, but there will be a large delay, so it is not suitable for applications that require high real-time performance; while the UDP protocol has a good real-time
  • If the network condition is not very good, you need to choose the TCP protocol. If the network condition is very good, choosing UDP can reduce the network load.

Guess you like

Origin blog.csdn.net/Chuangke_Andy/article/details/108391524