wood burning stoves 2.0*
The moose likes Java in General and the fly likes TCP or UDP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "TCP or UDP" Watch "TCP or UDP" New topic
Author

TCP or UDP

ParagS Kulkarni
Ranch Hand

Joined: Oct 04, 2004
Posts: 60
all of we have read in many java books that TCP is connection oriented protocol & UDP is connectionless protocol. but i have seen in most of the cases TCP is used.
in what sort of applications we have to use UDP?
or in other words in what situation using UDP is more beneficial than TCP?


Thanks, Parag
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Well, I'm no expert on this, but I'll have a go. Experts - get ready to shoot me down!

UDP is less heavyweight than TCP, so has the potential to offer better performance. However, if the sender needs to know when/if the message has been received, and needs to be sure of the order in which messages were received, TCP is necessary.

An example of when UDP could be used for better performance might be in a multi-player shoot-em-up game. Each player's process would be firing off UDP packets saying "my player is here" to all other players (or broadcast?). Each player's process would also be listening for such packets from other players. But the game could proceed perfectly happily if a few of these messages got lost or out-of-sequence. The worst you'd see is occasional jerky movement, which could be a worthwhile price to pay for the improved performance.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
correct. UDP packets have far less overhead so will give better performance but that comes at the price of never knowing whether packets are received at all and no guarantees that the packets arrive in the same order they were sent.

Many people are wary of that and choose to use TCP always, even in situations where UDP would suffice.


42
Scott Dunbar
Ranch Hand

Joined: Sep 23, 2004
Posts: 245
One issue with UDP is that a good many firewalls and/or bridges block some/all UDP traffic, especially when the SO_BROADCAST flag is set. On a trusted network this is not really an issue. But for internet connected hosts you can't realistically broadcast. TCP is easier to maintain statefull packet inspection. It can and is done with UDP but TCP, with the sequence numbers it maintains on packets, more naturally lends itself to this.

As Peter and Jeroen said the other issue is that the code is responsible for missed or out of order packets. Depending on you application that may or may not be important.


<a href="http://forums.hotjoe.com/forums/list.page" target="_blank" rel="nofollow">Java forums using Java software</a> - Come and help get them started.
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
Peter Chase:
UDP is less heavyweight than TCP, so has the potential to offer better performance.

It is a bit less heavy weight, but that's not the real issue. The packet overhead is small, and there are a tremendous number of things that TCP handles automatically. The only reason for using UDP is if you don't want the reliable connection that TCP provides.

However, if the sender needs to know when/if the message has been received, and needs to be sure of the order in which messages were received, TCP is necessary.

Especially the "if" part. When you send data with UDP, it may or may not get to the destination, and you won't know whether it does.

An example of when UDP could be used for better performance might be in a multi-player shoot-em-up game. Each player's process would be firing off UDP packets saying "my player is here" to all other players (or broadcast?). Each player's process would also be listening for such packets from other players. But the game could proceed perfectly happily if a few of these messages got lost or out-of-sequence. The worst you'd see is occasional jerky movement, which could be a worthwhile price to pay for the improved performance.

Kind of. In this case, TCP would probably be fine; with a dropped packet, you might receive two position on the resend whereas you'd only receive the more recent position with UDP, but in either case, you could use the more recent position. TCP might be a bit better because with UDP, out of order packets could result in a more recent position being overwritten by an older position. UDP would also be easier to hack (by screening out selected packets).

You're right that there are UDP applications on networked games, though. Perhaps a better example is with commands sent from the client to the server. If the client sends a "shoot" command followed by a "hide" command, and the first packet gets lost, you might prefer to only have the "hide" command get through, rather than have them both get through on a resend and end up hiding too late because the server still charged you for the time to "shoot" first.
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
Perhaps a better example of UDP usage would be a "simulcasting" radio station.

The radio station would use UDP to deliver it's content to whatever client was out there listening.

I have NEVER written a UDP application, and I have NO IDEA how radio stations actually deliver their content...I just always picture this scenerio whenever I read about UDP.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TCP or UDP
 
Similar Threads
how to minimize packet loss in UDP transmission
T3 protocol
TCP, UDP,IP
Connected vs. Unconnected protocol
SOAP and WebServices