The transport layer of the TCP/IP
model has two different protocols: the UDP
User Datagram Protocol and the TCP
Transmission Control Protocol.
TCP
and UDP
are protocols that operate at the transport layer.TCP
and UDP
requires open ports.TCP
is a connection-oriented protocol that provides full-duplex communication. It requires the establishment of a connection before data transmission, and the data payload is relatively larger.TCP
provides reliable delivery services, ensuring reliable communication using services like flow control and congestion control.20
bytes, and the maximum header size is 60
bytes, including source port, destination port, sequence number, acknowledgment number, data offset, control flags, window, checksum, urgent pointer, options, and other information.TCP
only supports one-to-one communication.TCP
is byte-oriented.TCP
ensures the order of data transmission by assigning a sequence number to each byte of the data stream within the TCP connection.TCP
provides features such as checksum, acknowledgment response, sequence number, timeout retransmission, connection management, flow control, and congestion control.TCP
is suitable for applications that require reliable transmission, such as file transfers.UDP
is connectionless, meaning that a connection does not need to be established before sending data, and the data payload is relatively smaller.UDP
provides best-effort delivery, meaning that it does not guarantee reliable delivery and does not use flow control and congestion control.8
bytes long, including source port, destination port, length, and checksum information.UDP
supports unicast, multicast, and broadcast functionalities, enabling one-to-one, one-to-many, many-to-many, and many-to-one data transmission.UDP
is message-oriented, and it does not merge or split the data segments received from the application layer. Instead, it preserves the boundaries of these segments and passes them down to the IP
layer after adding the header.UDP
does not ensure the order of data transmission, and the application layer program needs to control the order by adding sequence numbers to the data segments.UDP
adds minimal functionality on top of the IP
datagram service, mainly adding port functionality and error detection.UDP
is suitable for real-time applications such as network telephony, video conferencing, and live streaming.The transport layer cannot ensure the reliable transmission of data, and it can only be implemented at the application layer. This can be done by emulating the reliability transmission features of TCP, such as implementing acknowledgment mechanisms, retransmission mechanisms, and window confirmation, but at the application layer. Open-source programs like RUDP
, RTP
, and UDT
use UDP
to achieve reliable data transmission. Additionally, there are protocols like KCP
that balance the reliability of TCP
with the speed of UDP
.
Short connections: When a Client
sends a message to a Server
, the Server
responds to the Client
, and then the process is completed with a single read and write. At this point, either party can initiate a close
operation, but it is usually the Client
that initiates the close
operation. Short connections usually involve only one read and write operation between the Client
and the Server
. Managing short connections is relatively simple, as any existing connection is useful and does not require additional control measures. However, frequent client requests can result in wasted time and bandwidth on TCP connection establishment and closure operations.
Long connections: After a Client
and Server
complete a read and write operation, the connection between them does not close actively. Subsequent read and write operations continue to use this connection. Long connections can eliminate many TCP setup and closure operations, reduce waste, and save time. They are more suitable for clients that frequently request resources. However, as the number of client connections increases, the server load will also increase. In such cases, strategies such as closing long-unused connections using the LRU
algorithm, and limiting the number of client connections may be necessary to reduce the load.
Since TCP
is byte-oriented and cannot understand the business data of the higher layer, it is unable to guarantee that data packets are not split or recombined at the lower level. This issue can only be resolved through the design of the upper-layer application protocol stack.
0
if necessary). This way, the receiving end naturally splits each data packet by reading a fixed length of data from the receive buffer.