TCP and udp transmission comparison

TCP (Transmission Control Protocol, Transmission Control Protocol) is a connection-oriented protocol, that is, before sending and receiving data, a reliable connection must be established with the other party. A TCP connection must go through three "dialogs" to be established. The process is very complicated. Just briefly describe the simple process of these three dialogs: host A sends a connection request packet to host B: "I want to send you data , Is it okay?” This is the first conversation; host B sends to host A a data packet agreeing to connect and requesting synchronization (synchronization means that one of the two hosts is sending, the other is receiving, and coordinating work): "Yes, what do you Time to send it?" This is the second conversation; Host A sends another data packet to confirm host B's request synchronization: "I will send it now, you can continue!" This is the third conversation. The purpose of the three "dialogues" is to synchronize the sending and receiving of data packets. After three "dialogues", host A officially sends data to host B.

In detail:

TCP three-way handshake process
1 Host A sends a data segment containing a flag bit of the synchronization sequence number to host B to host B, and requests to establish a connection to host B. Through this data segment,
host A tells host B two things: I think To communicate with you; which serial number can you use as the starting data segment to respond to me.
2 After host B receives the request from host A, it uses an acknowledgement response (ACK) and synchronization sequence number (SYN) flag bit The data segment responds to host A, and also tells host A two things:
I have received your request and you can transmit data; which serial number do you want to use as the starting data segment to respond to me
3 Host A receives this After the data segment, send a confirmation response to confirm that the data segment of host B has been received: "I have received the response, and I want to start transmitting the actual data now

In this way, the 3-way handshake is completed, and host A and host B can transmit data.
Features of the 3-way handshake
There is no application layer data. The
SYN flag bit will only be set when the TCP connection is established.
After the handshake is completed, the SYN flag Bit is set to 0


TCP connection establishment requires 3 handshake, and disconnection requires 4 times

 

1 After host A completes the data transmission, it sets the control bit FIN to 1, and makes a request to stop the TCP connection.
2 Host B responds to it after receiving FIN, confirming that the TCP connection in this direction will be closed, and sets ACK to 1
3 By The B side proposes a closing request in the opposite direction again, setting FIN to 1
4 Host A confirms the request of Host B, sets ACK to 1, and the two-way closing ends.
It can be seen from the three-way handshake and four-time disconnection of TCP, TCP uses a connection-oriented communication method, which greatly improves the reliability of data communication, so that the sender
and receiver can interact before the data is officially transmitted, and lay a reliable foundation for the formal data transmission
. Explain
  the control of the ACK TCP header. One of the bits is to confirm the data. The confirmation is sent by the destination, and it is used to tell the sending end that the data segment before the serial number
has been received. For example, the confirmation number is X, which means that the first X-1 data segments have been received Arrived, only when ACK=1, the acknowledgment number is valid, when ACK=0, the acknowledgment number is invalid, then the data will be required to retransmit to ensure the integrity of the data.
SYN   synchronization sequence number, TCP will set this position when establishing a connection 1 The
FIN   sender completes the sending task bit. When TCP completes the data transmission and needs to be disconnected, the party that proposes to disconnect will set this bit to 1.

TCP header structure:
source port 16 bits,
destination port 16 bits,
serial number 32 bits,
response serial number 32 bits,
TCP header length 4 bits
reserved 6 bits,
control code 6 bits,
window size 16 bits,
offset 16 bits,
checksum 16 bits,
option 32 bits (Optional) In
this way, we get the minimum length of the TCP header, which is 20 bytes.

UDP (User Data Protocol, user datagram protocol)

(1) UDP is a non-connection protocol. The source and terminal do not establish a connection before transmitting data. When it wants to transmit, it simply grabs the data from the application and throws it on the network as quickly as possible. . On the sending end, the speed of UDP transmission of data is only limited by the speed at which the application generates data, the capacity of the computer, and the transmission bandwidth; on the receiving end, UDP puts each message segment in a queue, and the application receives it every time Read a message segment.

(2) Since the data transmission does not establish a connection, there is no need to maintain the connection status, including the receiving and sending status, so a server can transmit the same message to multiple clients at the same time.

(3) The header of the UDP information packet is very short, only 8 bytes, which is very small compared to the 20 byte information packet of TCP.

(4) Throughput is not regulated by the congestion control algorithm, but is only limited by the data rate, transmission bandwidth, source and terminal host performance of the application software.

(5) UDP uses best effort delivery, that is, reliable delivery is not guaranteed, so the host does not need to maintain a complicated link state table (there are many parameters).

(6) UDP is message-oriented . The UDP of the sender delivers the message delivered by the application program down to the IP layer after adding the header. It neither splits nor merges, but preserves the boundaries of these messages. Therefore, the application needs to select the appropriate message size.

我们经常使用“ping”命令来测试两台主机之间TCP/IP通信是否正常,其实“ping”命令的原理就是向对方主机发送UDP数据包,然后对方主机确认收到数据包,如果数据包是否到达的消息及时反馈回来,那么网络就是通的。

UDP的包头结构:
源端口 16位
目的端口 16位
长度 16位
校验和 16位

 

小结TCP与UDP的区别:

1.基于连接与无连接;
2.对系统资源的要求(TCP较多,UDP少);
3.UDP程序结构较简单;
4.流模式与数据报模式 ;

5.TCP保证数据正确性,UDP可能丢包,TCP保证数据顺序,UDP不保证。

Guess you like

Origin blog.csdn.net/junzhu_beautifulpig/article/details/50776597