UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2002-07-26 11:28 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: 5.1 - When should I use UDP instead of TCP?

6

UDP is good for sending messages from one system to another when the order isn't important and you don't need all of the messages to get to the other machine. This is why I've only used UDP once to write the example code for the faq. Usually TCP is a better solution. It saves you having to write code to ensure that messages make it to the desired destination, or to ensure the message ordering. Keep in mind that every additional line of code you add to your project in another line that could contain a potentially expensive bug.

If you find that TCP is too slow for your needs you may be able to get better performance with UDP so long as you are willing to sacrifice message order and/or reliability.

Philippe Jounin would like to add...

    In chapter 5.1 you say UDP allows more throughput than TCP. It is rarely the case if you have to pass several routers.
    For instance, if you connect two LANs via X25 (a common way in Europe!), every UDP datagram will :

    *  establish a Virtual Channel (VC)
    *  send the data
    *  close the VC,

    whereas the VC remains during a TCP dialog.

UDP must be used to multicast messages to more than one other machine at the same time. With TCP an application would have to open separate connections to each of the destination machines and send the message once to each target machine. This limits your application to only communicate with machines that it already knows about.

From: Nick Lockyer

Actually UDP and TCP both have the same throughput, since they are both going over the same hardware. However, TCP packets are larger than UDP packets because they have extra fields in them, thus by definition sending a TCP packet takes longer than a UDP packet because it is bigger. TCP also has a three way handshake (packet goes from A->B, acknowledgement from B->A, then another ACK A->B). This takes a certain amount of time. That is why TCP takes longer than UDP.

From: Arun

Difference between TCP/IP and UDP/IP
------------------------------------
Both TCP & UDP are transport layer protocols.

TCP
---
* TCP is a connection oriented protocol.The meaning for connection establishment is that client and server make sure there identity to each other through a well define 3 way handshake.
* During handshake they ensure there data handling capacity by sending there Maximum Trasmission Unit (MTU) size to each other.
* Both client and server send there packet sequence number to each other.
* TCP ensure the reliable data transfer through acknowledge.For each and every packet there send an acknowledge.
* It is always recommended to use TCP for huge and relaible data transfer.
* The packet will always take a single route to reach the destination, there will not be any duplicate packets reach the destination.
* The possibality of duplication in TCP is, if a packet cought in a loop before reaching the destination due to some intermediate router or path problem, so it can't reach the destination in the estimated RTT(Round trip time) that packet will be retransmitted and the packet in loop also reach the destination.Here destination have duplicate
packets.TCP handle this duplication issue well.
UDP
---
* UDP is connection less data transfer, it means there will not be any handshake between peers.Server will listen at some port and accept the packet to the port from any client.
* Unreliable data transfer, there is no acknowledge for data.
* UDP packet take any path to reach the destination.So there is a possibality of duplicate packets to reach the destination.It will take the first packet and discard the duplicates.
* Packet ordering is not important for UDP, it will accept packets in any order finally it will rearrage by it packet sequence number.
* UDP is much faster than TCP.
* .It is always recommanded that UDP is used of small data transfers.UDP are used to get the Network management stastical detail from SNMP Agents or get the IP address from the DNS Server giving Host Name.UDP packets are low priority packets in the network.
From: mikec

Use UDP when reliable delivery is not a critical
issue - otherwise incur the overhaed of the 3 step
handshake and the SYN/ACK flags to gett slower yet
more reliable communications ...

Offline

Board footer

Powered by FluxBB