When should I use UDP instead of TCP?

Answer Posted / chaitanya

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.

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.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How many sockets can a port have?

650


What's better 6pt or 12pt sockets?

619


Are unix sockets faster than tcp?

647


What are the pros/cons of select(), non-blocking I/O and SIGIO?

1119


Whats the difference between select() and poll()?

1389


How are unix domain sockets implemented?

643


How can I write a multi-homed server?

1409


How does a socket work?

644


Where is the socket located?

648


What is socket programming in java?

641


Why does it take so long to detect that the peer died?

1305


How does unix socket work?

664


What is a socket address?

688


What is a deep well socket?

702


How can I tell when a socket is closed on the other end?

1123