Difference between TCP and UDP?

140,607

Solution 1

TCP is a connection oriented stream over an IP network. It guarantees that all sent packets will reach the destination in the correct order. This imply the use of acknowledgement packets sent back to the sender, and automatic retransmission, causing additional delays and a general less efficient transmission than UDP.

UDP is a connection-less protocol. Communication is datagram oriented. The integrity is guaranteed only on the single datagram. Datagrams reach destination and can arrive out of order or don't arrive at all. It is more efficient than TCP because it uses non ACK. It's generally used for real time communication, where a little percentage of packet loss rate is preferable to the overhead of a TCP connection.

In certain situations UDP is used because it allows broadcast packet transmission. This is sometimes fundamental in cases like DHCP protocol, because the client machine hasn't still received an IP address (this is the DHCP negotiaton protocol purpose) and there won't be any way to establish a TCP stream without the IP address itself.

Solution 2

From the Skullbox article:

TCP (Transmission Control Protocol) is the most commonly used protocol on the Internet. The reason for this is because TCP offers error correction. When the TCP protocol is used there is a "guaranteed delivery." This is due largely in part to a method called "flow control." Flow control determines when data needs to be re-sent, and stops the flow of data until previous packets are successfully transferred. This works because if a packet of data is sent, a collision may occur. When this happens, the client re-requests the packet from the server until the whole packet is complete and is identical to its original.

UDP (User Datagram Protocol) is anther commonly used protocol on the Internet. However, UDP is never used to send important data such as webpages, database information, etc; UDP is commonly used for streaming audio and video. Streaming media such as Windows Media audio files (.WMA) , Real Player (.RM), and others use UDP because it offers speed! The reason UDP is faster than TCP is because there is no form of flow control or error correction. The data sent over the Internet is affected by collisions, and errors will be present. Remember that UDP is only concerned with speed. This is the main reason why streaming media is not high quality.

1) TCP is connection oriented and reliable where as UDP is connection less and unreliable.

2) TCP needs more processing at network interface level where as in UDP it’s not.

3) TCP uses, 3 way handshake, congestion control, flow control and other mechanism to make sure the reliable transmission.

4) UDP is mostly used in cases where the packet delay is more serious than packet loss.

Solution 3

Think of TCP as a dedicated scheduled UPS/FedEx pickup/dropoff of packages between two locations, while UDP is the equivalent of throwing a postcard in a mailbox.

UPS/FedEx will do their damndest to make sure that the package you mail off gets there, and get it there on time. With the post card, you're lucky if it arrives at all, and it may arrive out of order or late (how many times have you gotten a postcard from someone AFTER they've gotten home from the vacation?)

TCP is as close to a guaranteed delivery protocol as you can get, while UDP is just "best effort".

Solution 4

Reasons UDP is used for DNS and DHCP:

DNS - TCP requires more resources from the server (which listens for connections) than it does from the client. In particular, when the TCP connection is closed, the server is required to remember the connection's details (holding them in memory) for two minutes, during a state known as TIME_WAIT_2. This is a feature which defends against erroneously repeated packets from a preceding connection being interpreted as part of a current connection. Maintaining TIME_WAIT_2 uses up kernel memory on the server. DNS requests are small and arrive frequently from many different clients. This usage pattern exacerbates the load on the server compared with the clients. It was believed that using UDP, which has no connections and no state to maintain on either client or server, would ameliorate this problem.

DHCP - DHCP is an extension of BOOTP. BOOTP is a protocol which client computers use to get configuration information from a server, while the client is booting. In order to locate the server, a broadcast is sent asking for BOOTP (or DHCP) servers. Broadcasts can only be sent via a connectionless protocol, such as UDP. Therefore, BOOTP required at least one UDP packet, for the server-locating broadcast. Furthermore, because BOOTP is running while the client... boots, and this is a time period when the client may not have its entire TCP/IP stack loaded and running, UDP may be the only protocol the client is ready to handle at that time. Finally, some DHCP/BOOTP clients have only UDP on board. For example, some IP thermostats only implement UDP. The reason is that they are built with such tiny processors and little memory that the are unable to perform TCP -- yet they still need to get an IP address when they boot.

As others have mentioned, UDP is also useful for streaming media, especially audio. Conversations sound better under network lag if you simply drop the delayed packets. You can do that with UDP, but with TCP all you get during lag is a pause, followed by audio that will always be delayed by as much as it has already paused. For two-way phone-style conversations, this is unacceptable.

Solution 5

One of the differences is in short

UDP : Send message and dont look back if it reached destination, Connectionless protocol
TCP : Send message and guarantee to reach destination, Connection-oriented protocol

Share:
140,607

Related videos on Youtube

user749414
Author by

user749414

Updated on April 02, 2021

Comments

  • user749414
    user749414 about 3 years

    What is the difference between TCP and UDP?

    I know that TCP is used in the case of non-time critical applications, and UDP is used for games or applications that require fast transmission of data. I know that TCP is used for HTTP, HTTPs, FTP, SMTP, and Telnet. I know that UDP is used for DNS and DHCP.

    But why? What characteristics of TCP and UDP make it useful for their respective use cases?

    • user1066101
      user1066101 about 13 years
      And this (skullbox.net/tcpudp.php) -- which was the first Google hit -- wasn't clear enough? What was confusing about it? Maybe this is better? tcpipguide.com/free/…
    • MattH
      MattH about 13 years
      I'm really curious why this question got (at time of writing) 3 upvotes. The first sentence doesn't even make sense and there is a lot of material available on this topic if one searches.
    • ire_and_curses
      ire_and_curses about 13 years
    • ire_and_curses
      ire_and_curses about 13 years
      @MattH: 1) It's a good question, if rather broad and a duplicate already well-answered. 2) You had more than enough reputation to fix the typo in the first sentence. 3) It's irrelevant that information about this exists elsewhere. Stack Overflow aims to become a repository of knowledge, and answers questions canonically here.
    • Heath Hunnicutt
      Heath Hunnicutt about 13 years
      Interesting that almost nobody mentions that DHCP uses broadcast, but everybody thinks 'the answer' is about guarantee of delivery and retransmission.
    • Manny265
      Manny265 over 10 years
      FYI DNS uses both TCP and UDP
    • Alan006
      Alan006 over 9 years
      Just for anyone else reading this in the future, the Skullbox site mentioned above has MALWARE according to Google (it stopped me when I clicked on it). I'd advise not going there.
  • Erik Nedwidek
    Erik Nedwidek about 13 years
    Decent enough answer. I would add that in a TCP stream the packets are acknowledged by the destination and corrupt packets/missing packets are resent by the sender. In UDP the packets are sent and the destination receives them in any order and does not acknowledge receipt.
  • MattH
    MattH about 13 years
    Bit of a misleading analogy perhaps more suited to QoS
  • MattH
    MattH about 13 years
    +1 Reasonably good summary. Though the is the most commonly used protocol on the Internet statement is arguable and really depends on how you define most commonly used, protocol and the Internet. For example, Internet Protocol is a more likely contender to that particular crown.
  • Heath Hunnicutt
    Heath Hunnicutt about 13 years
    -1: The reason UDP is used for DHCP has nothing to do with packet delay or loss.
  • supercat
    supercat about 11 years
    When sending lots of data on a clear channel, TCP is often faster than UDP. The reason UDP is used for things like live-streaming audio or video is that when a TCP packet goes missing, the receiving application won't see anything more until the missing data has been retransmitted and received successfully. In many streaming applications, data which arrives late will be useless, so there's no point holding everything up while waiting for a retransmission which is going to be useless anyway.
  • Niko Bellic
    Niko Bellic almost 10 years
    An example where UDP is used is in video and audio transmission where losing a few packets here and there usually does not matter so much (the color of a frame might be off, or a tiny nano-second of audio might be cut out or altered -- not really noticeable to humans). Of course, if your connection is really bad, you might lose so many packets that the video appears blurry/pixelated and the audio becomes fuzzy and cuts in and out a lot.
  • John Smith
    John Smith over 8 years
    i like the analogy a lot, but one thing this answer slightly misrepresents is the speed. it makes it sound like TCP is faster, when in reality UDP is because there is less overhead.
  • ScottSmudger
    ScottSmudger over 7 years
    UDP is used for DHCP because TCP does not support broadcasts. DHCP relies on the use of a broadcast in order to get the IP address for the DHCP server. See stackoverflow.com/questions/21266008/…