Comparison between UDP protocol and TCP protocol

Source: Internet
Author: User
Tags ftp protocol

UDP is a non-connection-oriented, unreliable, unordered, and traffic-free transport layer protocol. Each datagram sent by UDP is a record-type datagram, the so-called record-type datagram is the record boundary that the receiving process can recognize the received datagram. TCP is a connection-oriented, reliable, ordered, and traffic-controlled transport layer protocol. It is a byte stream protocol with no record boundaries.

1. Record and byte stream

UDP protocol: when sending each datagram, the sending process does not wait for multiple datagram to be centralized and sent out with a large datagram. Instead, it is immediately sent out. It is a record-type protocol. And each time the receiving process passes the read or recv ...... The obtained datagram must be the datagram sent by the sending process. It cannot be multiple datagram. The receiving process can identify the record boundary of each datagram sent by the sending process.

TCP protocol: when sending each datagram, the sending process may not send it out immediately during kernel processing. Instead, it will combine multiple datagram to send a large datagram, it is a byte stream protocol. The receiving process reads the datagram sent by the sending process through read each time. It does not necessarily mean that the sending process originally sent the datagram. The receiving process cannot identify the record boundary of each datagram, therefore, the TCP protocol is a byte stream protocol with no record boundaries.

For example, the protocol used for QQ chat should have a record boundary. In the chat process, the unit is "message". A message can be considered as a record, therefore, the QQ chat protocol adopts the UDP protocol instead of the TCP protocol.

2. order and disorder

UDP protocol: Each datagram sent by the sending process does not arrive at the receiving process in the original sending order. It is possible that the datagram sent earlier may arrive at the receiving process. When a datagram is transmitted through an intermediate path, the data is transmitted in different paths or for other reasons. UDP is an unordered transmission protocol. So in order to make UDP-based applications orderly, you must set the serial number and validation mechanism in the application to make them orderly.

TCP protocol: ordered protocol with timeout, serial number, retransmission, and validation mechanisms.

For example, the FTP protocol is used to transfer files. To ensure that each Datagram Protocol transmitted receives the file content in an orderly manner, the FTP protocol is based on the TCP protocol.

Why is the TFTP protocol based on UDP? To ensure the order, validation and serial number fields are introduced in the TFTP protocol.

There is another problem here. The content transmitted by the control connection in the FTP protocol seems to be in the message form. The client sends a request message on the control connection, and the server returns a request result message, I feel that the FTP control connection should adopt the UDP protocol. Why does the TCP protocol apply? Because the control connection is interactive message transmission, after the client sends a request, the client will not send the second request message until the server's response message arrives, so don't worry that the two request messages will overlap. That is, TCP can also be used for interactive message transmission.

3. Traffic Control

UDP protocol: there is no traffic control mechanism. If the sending process sends a datagram filled with the receiving buffer of the receiving process, the datagram is discarded. In this case, UDP does not notify the sending process to slow down the data transmission rate.

TCP protocol: with traffic control.

4. Client Communication Process Comparison

4.1 client connection process comparison

After creating a plug-in, UDP can establish communication with multiple servers. However, TCP can only communicate with one server. TCP does not allow the target address to be a broadcast or multicast address, UDP allowed. The communication relationship between the UDP client and the server can be one-to-many, while the TCP protocol can only be one-to-one.

Of course, UDP can also specify the IP address and port (corresponding to the ③ operation in 1) of the peer through connect, just like TCP. connect is an interport connection operation, after the connect operation, the corresponding plug-in is connected. Unlike the TCP protocol, the UDP connect implementation does not include a three-way handshake. Whether it is UDP or TCP, the common part of connect implementation includes: if the local address and port of the specified plug-in are not specified, then, during connect, the kernel specifies the local address and local port for it. The kernel determines the outbound Interface Based on the destination address in the plug-in, and then specifies the outbound interface IP address as the local address of the plug-in. After UDP uses the connect operation, the communication relationship with the server becomes one-to-one, instead of one-to-many. In this case, the destination address cannot be a broadcast or multicast address, because the connect function does not allow the target address to be a broadcast or multicast address. After UDP is connected, you do not need to specify the destination address and port when sending data reports through sendto. If the destination address and port are specified, an error is returned. You can specify multiple connect operations for the same plug-in through the UDP protocol. However, the TCP protocol is not. TCP can only specify one connect operation. After the second connect operation is specified in UDP, the first connection is broken and the second connection is established.

When the client establishes a connection with the server, the first step is to establish a connection socket through socket, and then bind the local address and local port through bind. Of course, the binding operation does not need to be specified.

UDP protocol: If no binding operation is specified, you can use the following connect operation to bind the plug-in to the kernel. If connect is not specified, therefore, the binding operation has to specify the destination address and port through the write operation (sendto and sendmsg) of the plug-in. In this case, the local address of the plug-in is not specified, and the local port is specified by the kernel, after the first sendto operation, the local port of the plug-in will not be changed after it is specified by the kernel.

TCP protocol: If no binding operation is specified, the kernel can bind the plug-in through the following connect operation. The kernel determines the outbound Interface Based on the destination address in the plug-in, and then specifies the IP address of the outbound interface as the local address of the plug-in. The Connect operation is required for TCP clients and must be specified.

(Whether it is UDP or TCP, the corresponding plug-in is connected after the connect operation, without the connect operation, it indicates that the plug-in is not connected .)

When binding a local address or port through bind, whether it is a connected or unconnected plug-in, if the local port of a plug-in is the same as the local port to be bound by the user, EADDRINUSE (Address already in use) error is returned. To bind a port with the same local port as an existing plug-in, you must first set the plug-in option SO_REUSEADDR and then bind it. In linux, if the bound local address is different and the local port is the same, you do not need to set the plug-in option SO_REUSEADDR, for other UNIX-like systems, you must set the SO_REUSEADDR plug-in option in advance according to the unix network programming.

The local address and port bound to the TCP protocol cannot be the same as the existing plug-in (whether connected or not. For UDP, You can bind the same local address and local port by setting the plug-in option SO_REUSEPORT. In linux, The SO_REUSEPORT option is not available. Therefore, in linux, UDP protocol and TCP protocol do not allow two sockets with the same local address and port.

There is also A big difference between the TCP protocol and the UDP protocol: for example, A multi-host machine has IP addresses A, B, and C, now we create four identical TCP listening port, corresponding four plug-in address structures (*, port), (A, port), (B, port), (C, port). Now, if A client wants to establish A connection with (A, port), it will only establish A connection with the (A, port) plug-in, rather than establishing A connection with the plug-in with A wildcard address. If you create four identical UDP listening port, the corresponding four plug-in address structures (*, port), (A, port), (B, port), (C, if A client needs to establish communications with (A, port), the data sent to (A, port) will also be copied to the (*, port) plug-in. Cause: the communication between the TCP protocol is one-to-one, while UDP can be one-to-many.

Procedure

UDP protocol

TCP protocol

Socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)

Socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)

Bind local address and local port can be ignored. In the following connect or the first sendto is specified by the kernel)

Bind the local address and local port can be ignored. In the following connect operation, the kernel specifies the local address and port)

Connect specifies the peer address and port. When establishing a connection, you can ignore the address and port specified by sendto)

Connect specifies the peer address and port to establish a connection

Read or write operations

Read or write operations

Figure 1 client connection process

4.2 server connection process comparison

There is no essential difference between a UDP client and a server. Each UDP client is also a server. The TCP protocol is different. The TCP protocol must apply for a listener through listen, and then accept a client connection through accept, when a client connection is received, a separate plug-in is created to communicate with the client. That is to say, the server is monitored by a separate listening plug-in for client connection requests, after receiving a connection request from a client, the server creates another plug-in to communicate with the client.

When binding a local address and a local port through bind, the server should pay attention to the same situation as the client.

Procedure

UDP protocol

TCP protocol

Socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)

Socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)

Bind local address and local port can be ignored. In the following connect or the first sendto is specified by the kernel)

Bind the local address and local port can be ignored. In the following listen operation, the kernel specifies the local address and local port)

Connect specifies the peer address and port. When establishing a connection, you can ignore the address and port specified by sendto)

Listen listens to client connections

Read or write operations

Accept accepts client connections

 

Read or write operations

Figure 2 server connection process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.