The basic principles of network protocols TCP and UDP

Introduce

  • The transport layer protocols of the TCP/IP protocol suite mainly include TCP and UDP
  • TCP is a reliable connection-oriented transport layer protocol. It supports connection-oriented reliable data transmission on unreliable networks
  • UDP is a connectionless transmission protocol, mainly used to support data transmission on more reliable links, or for applications that are more sensitive to delay

The role of TCP/IP transport layer

  • Provide connection-oriented or connectionless services
  • Maintain connection status
  • Segment and encapsulate application layer data
  • Multiplexing
  • Reliable data transmission
  • Perform flow control

Basic principles of TCP

Features

  • Three-way handshake -> establish a reliable connection
  • Confirmation mechanism -> response reception
  • Port number -> multiplexing
  • Serial number -> Loss detection, disorderly rearrangement
  • Integrity check -> error detection
  • Window Mechanism -> Flow Control

TCP encapsulation
Insert picture description here
TCP/UDP port number
Insert picture description here
TCP establishment process

set up

The   
  Insert picture description here
editor of HostA recommends his own Linux and C/C++ technical exchange group: [960994558] I have compiled some learning books and video materials that I think are better for sharing (including C/C++, Linux, Nginx, ZeroMQ, MySQL, Redis, fastdfs, MongoDB, ZK, streaming media, CDN, P2P, K8S, Docker, TCP/IP, coroutine, DPDK, etc.), you can add it yourself if you need it! ~
Insert picture description here

tear down

HostA
  Insert picture description here
transmission confirmation
Insert picture description here
timeout retransmission
Insert picture description here
sliding window
Insert picture description here
three-way handshake process description:

  • The client sends a request message for establishing a TCP connection. The message contains the seq sequence number, which is randomly generated by the sender, and the SYN field in the message is set to 1, indicating that a TCP connection needs to be established. (SYN=1, seq=x, x is a randomly generated value)

  • The server responds to the TCP connection request message sent by the client, which contains the seq sequence number, which is randomly generated by the responder, and the SYN is set to 1, and an ACK field is generated. The ACK field value is sent from the client Based on the sequence number seq, add 1 to reply, so that when the client receives the information, it knows that its TCP establishment request has been verified. (SYN=1, ACK=x+1, seq=y, y is a randomly generated value) Here ack plus 1 can be understood as confirming who to establish a connection with.

  • After the client receives the TCP establishment verification request sent by the server, it will increase its sequence number by 1 and reply to the ACK verification request again, adding 1 to the seq sent by the server to reply. (SYN=1, ACK=y+1, seq=x+1)

Description of the four waved process:

  • The client sends a TCP connection request message, which contains the seq sequence number, which is randomly generated by the sender, and also sets the FIN field in the message to 1, indicating that the TCP connection needs to be disconnected. (FIN=1, seq=x, x is randomly generated by the client)
  • The server will reply to the TCP disconnect request message sent by the client, which contains the seq sequence number, which is randomly generated by the replying side, and will generate an ACK field. The value of the ACK field is based on the seq sequence number sent by the client Add 1 to reply so that when the client receives the message, it knows that its TCP disconnect request has been verified. (FIN=1, ACK=x+1, seq=y, y is randomly generated by the server)
  • After the server has responded to the client's TCP disconnect request, it will not immediately disconnect the TCP connection. The server will first ensure that all the data transmitted to A has been transmitted before the disconnection. Once the data transmission is confirmed, it will The FIN field of the reply message will be set to 1, and a random seq sequence number will be generated. (FIN=1, ACK=x+1, seq=z, z is randomly generated by the server)
  • After the client receives the TCP disconnect request from the server, it will reply to the server disconnect request, including the randomly generated seq field and ACK field. The ACK field will add 1 to the seq of the server TCP disconnect request to complete the service Verification reply requested by the client (FIN=1, ACK=z+1, seq=h, h is randomly generated by the client)

So far, the 4 waves of TCP disconnection are completed

Basic principles of UDP

UDP encapsulation
Insert picture description here
TCP and UDP comparison
Insert picture description here
summary

  • TCP and UDP identify upper-layer applications and services through port numbers
  • TCP establishes a reliable connection through a three-way handshake
  • TCP performs error checking through checksum, reliable transmission through sequence number, confirmation and timeout retransmission mechanism, and flow control through sliding window
  • UDP is simple to implement, consumes less resources, and has strong real-time performance, which is suitable for highly reliable networks and delay-sensitive applications

Finally, if you find it difficult to find learning materials, you can add the editor's C/C++ exchange group: 960994558 The learning materials have been shared in the group, and we look forward to your joining~
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_52622200/article/details/111560990