Network protocol details TCP / UDP (1)


Introduction – the beginning

  • I haven’t been in the Internet industry for a long time, but I’m also a self-taught person. I’ve been working hard for the next day every day, and I’ve forgotten my original intention and the fun of programming. But because of this year’s special reasons, I’m calm. I decided to organize and push materials irregularly, help future generations to solve problems, and develop some open-source projects to simplify the code. I hope that I hope you don’t have the same difficulties as me, you can also add my wechat to provide you with some available information free of charge;
  • There is not much nonsense. This article explains TCP and UDP to help you better understand and deal with the interview questions



  • Connection oriented, reliable, byte stream based communication protocol;
  • The data flow of application layer is divided into segments and sent to TCP layer of target node
  • Each packet has a serial number. If it is received by the other party, it will return to ack. If it is not received, it will be retransmitted;
  • Use checksums to verify that data is in error during transmission
  • TCP does not contain IP but will have active and destination ports

TCP header

Network protocol details TCP / UDP (1)

  • Source port and destination port account for two bytes respectively (the focus of communication is between application processes, local is based on PID, cross server is TCP + IP + port number)
  • Sequencenumber s / N: each byte of 4-byte transmission has a s / N and is numbered in sequence, for example, one message carries 100 bytes from 101; the next message segment is 101 + 100
  • How far is the data offset from the starting message
  • Window: sliding window, used to inform the sender and receiver of cache size
  • Checksum: stored in 16 bits; calculated by the sender and verified by the client
  • Urgent pointer: the current pointer is valid only when the emergency pointer urg is 1;
  • Tcpflags: there are eight flag bits
    1. Urg: emergency pointer flag; valid when 1 and ignored when 0
    2. Ack: confirm the serial number flag. When it is 1, it is valid; when it is 0, it is invalid;
    3. PSH: push flag, the same as above. When the data with the push flag bit is applied and processed as soon as possible, it will not be queued in the buffer
    4. RST: reset the link flag, reset a series of wrong links due to the downtime of the host, or reject some illegal filers and wrong requests
    5. Syn: synchronization serial number; used to establish the link process. When syn = 1 ack = 0, it represents the confirmation domain without using the piggyback, and use it is ack = 1;
    6. Fin: finish, used to release the link. When it is 1, it means that the sender has no data to send;

    Flow chart of three handshakes

    Network protocol details TCP / UDP (1)

    • 1. First handshake: the message segment called syn does not carry data, but it needs to consume a serial number, and req is a positive integer;
    • 2. Second handshake: no data and confirmation message
    • 3。 Third Handshake: the last confirmation, TCP establishes a link, and can carry data
    • The reasons for TCP three-way handshake: initialize the sequencenumber sequence number value; to ensure that there is no disorder caused by network transmission;
    • There is a hidden danger of syn timeout in the first handshake, and the server receives syn from the client. When replying, it fails to receive ack confirmation. The server will try again until the timeout. The default timeout time of Linux is 63s, which is easy to cause synflood attack. Measures: when the syn queue is full, it will pass TCP_ Syncookies parameter sends back syncookie (including source port, destination port and time stamp as the value of SEQ); if it is a normal linked client, it sends back syncookie and directly establishes a link
    • TCP is equipped with a live protection mechanism. When the client suddenly disconnects, it will send a live protection detection message to the other party, until the number of messages reaches the threshold value, the link will be disconnected

It’s too late. We will keep updating next time;
I hope you will forge ahead and never forget your original heart;
I wish you all success in your career and good company;

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]