The difference between TCP/IP and UDP

Source: Internet
Author: User

First, the biggest difference between TCP/IP and UDP is that TCP/IP is connection-oriented and UDP is non-connection-oriented. Popular point is: TCP/IP pipe to the pipe, UDP pipe hair no matter. Therefore, in terms of security, TCP/IP is more advantageous.

Second: TCP/IP is biased toward point-to-point communication, the difference between the server and the client is obvious, while UDP can use multicast to achieve a one-to-many, communication is more like a group chat system, there is little difference between the client and the server, in time and space with higher savings. When using multicast, identify a class D address as the address of the group, add the native to the group, send a message to the group, and receive the message from the group.

Third: In the specific code structure, TCP/IP is to create a socket object to connect, from the connection object to get input and output stream, read and write in the stream to communicate. UDP, in turn, creates a Datagramsocket object in the native computer, wraps the data into a datagrampacket packet and sends the packet with the address to which it is sent.


TCP (transmission Control Protocol, transmission Protocol) is a connection-based protocol, which means that a reliable connection must be established with each other before the data is formally sent and received. A TCP connection must go through three "dialogues" to build up, the process is very complex, we only do a simple, image of the introduction, you just can understand the process. Let's take a look at the simple process of these three conversations: Host A sends a connection request packet to Host B: "I want to send you data, OK." "This is the first conversation; Host B sends a consent connection to host A and requires synchronization (synchronization is the two host one in the send, one in the receiving, coordination work) packet:" Yes, when do you send. " "This is the second conversation; Host a then sends a packet to confirm that Host B's requirements are synchronized:" I'll send it now, you go on. " ", this is the third dialogue. Three times the purpose of the "conversation" is to synchronize the sending and receiving of packets, and after three "conversations", host a formally sends the data to Host B.

The TCP protocol can provide a reliable communication connection for the application, so that the byte stream emitted by one computer is sent to other computers on the network without error, and the data communication system with high reliability requirements often uses the TCP protocol.

To non-connected "is not necessary to establish a connection with the other party before the official communication, regardless of the status of the other party directly sent. This is very similar to the current popular mobile phone messages: When you send text messages, you only need to enter the other mobile phone number is OK.

UDP (User data Protocol, Subscriber Datagram Protocol) is the protocol that corresponds to TCP. It is a non-connected protocol that does not establish a connection with the other, but sends the packet in the past directly.

For example, we often use the "ping" command to test the TCP/IP communication between the two hosts is normal, in fact, the principle of "ping" command is to send UDP packets to the other host, and then the other host to confirm the receipt of the packet, if the packet arrives timely feedback back, then the network is through. For example, in the default state, a "ping" operation sends 4 packets (as shown in Figure 2). As you can see, the number of packets sent is 4 packets, and the received is also 4 packets (because the other host receives a confirmation of the received packet back). This fully illustrates that the UDP protocol is for a non-connected protocol, and there is no process for establishing a connection. Because the UDP protocol does not have a connection process, it has high communication performance, but because of this, its reliability is not as high as the TCP protocol. QQ uses UDP to send messages, so there are times when messages are not received.

Schedule: Differences between the TCP protocol and the UDP protocol

TCP protocol and UDP protocol have their own merits and short, and are suitable for different requirements of communication environment. The difference between the TCP protocol and the UDP protocol is shown in the schedule.

The basic function of IP protocol is to provide data transmission, packet coding, packet routing, segmentation and so on. With IP-coding conventions, data can be successfully routed to the correct network or subnet via routing. An IP protocol is a non-connected protocol.

Connection-oriented TCP, which is a connection-based protocol, means that a reliable connection must be established with the other party before it is formally sent and received, which provides reliable data transfer and ensures accurate arrival to the destination.

For a non-connected UDP protocol, he does not establish a connection to the other, but sends the packet over directly. This agreement does not guarantee the accurate arrival of the data. UDP is suitable for only a small amount of data at a time, the reliability of the application environment is not high, for example, we often use "ping" to test the two host communication is normal, in fact, the command is sent to the other side of the UDP packet, and then the other host to confirm the receipt of the packet, if the packet arrives in time, It means the network is a pass.

QQ is the UDP message, so sometimes there will be no information received when

In addition, the UDP program structure is relatively simple, TCP to system resources more than the requirements of UDP

The UDP and TCP protocols are the Protocols of the transport layer, and the IP protocol is the protocol of the network layer.

TCP connection requires three handshake process, 4 times disconnection required

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.