1. TCP and UDP
The transport layer of the Internet has two main protocols that complement each other. One is TCP protocol and the other is UDP protocol.
UDP: User Datagram Protocol, is a connectionless protocol for data transmission.
TCP: Transmission Control Protocol, is a connection-oriented protocol for data transmission.
2. The difference between TCP and UDP
TCP | UDP |
---|---|
Connection-oriented | Connectionless |
Byte stream oriented | Message-oriented |
Reliable, data can be transmitted intact and without loss | Unreliable, data is easily lost in transmission |
Only 1 to 1 | Support 1 to 1, 1 to many |
The first byte is 20 | The first byte is 8 |
3. What kind of scenario is it suitable for?
The advantage of UDP to TCP is its fast transmission speed. When the emphasis is on transmission performance rather than integrity, we will use UDP, such as audio and multimedia applications, DNS, TPTP, SNMP, etc.
In other cases, TCP is basically used. TCP ensures data integrity. In addition to being slower than UDP, UDP and some TCP have all.
4. TCP's three-way handshake and four waved hands
To understand this aspect, we must first understand the TCP message.
Here we have to understand that several keywords are ACK, SYN, FIN, seq, and ack.
ACK: The TCP protocol stipulates that it is valid only when ACK=1. It also stipulates that the ACK of all messages sent after the connection is established must be 1.
SYN (SYNchronization): Used to synchronize the serial number when the connection is established. When SYN=1 and ACK=0, it indicates that this is a connection request message. If the other party agrees to establish a connection, it should set SYN=1 and ACK=1 in the response message. Therefore, setting SYN to 1 means that this is a connection request or connection acceptance message.
FIN (finis) means end, which means end, and is used to release a connection. When FIN = 1, it indicates that the data of the sender of this segment has been sent, and the connection is required to be released
seq: sequence number.
ack: Confirmation number
4.1 Three-way handshake
Why are there three handshake? In one sentence, when the client and the server establish a connection, at least three handshake is required until each can send and receive.
The client sends a connection request to the server, the server is awakened, and wants to establish a connection with the client and reply that the client request has been received, and the client receives the request and replies that the client has accepted it.
Let's simulate the scene in the form of vernacular Chinese:
c "Are you there?" and patted s
s "I am here, are you still there?"
c "I am here."
4.2 Four waves
Why did ACK and FIN not be sent together, but two more steps?
Because it is possible that when the client sends a FIN disconnect request, the server has not finished receiving the data sent by the client before. The server first replies that the client has received the information, and then disconnects with the client after receiving it.
Vernacular:
c "I'm going to disconnect from you"
s "I know, wait until I finish receiving the things you sent me before, wait a minute"
s "I'm fine, let's disconnect"
c "I know "