UDP over TCP
I am wondering wether or not I should use UDP in my game instead of TCP. Every one says that is is "faster", but how much? Is it worth the trouble involved? Also, how does it work? In TCP you connect to a remote host by their IP address. But with UDP, it looks like you just open up a port. Does this mean that when you send a packet, all clients with that port open receive the data? If this is the case, then after sending enough data in your packets to get them in the right order and to the right client, is it really faster than TCP?
Thanks for your time!
I just read that some games use UDP and TCP. They use UDP to send data that doesn't matter too much, and TCP to send critical data. Does this answer my question about speed? Is UDP soooo much faster that they goto all this trouble? Is it really allot of trouble?
When you send data with UDP you still specify an IP and port to send to. You just don't "connect" to an IP because it is a "connectionless" protocol.
Basically with TCP you have the following steps:
Server: create TCP socket
Server: bind to port
Server: set to listen state
Client: create TCP socket
Client: connect to server's IP/Port
Server: accept connection from client
Client and Server: Send and receive data as necessary
With UDP you typically have steps like so:
Server: Create UDP socket
Server: Bind to port
Client: Create UDP socket
Client: Bind to port
// Repeat the following as necessary...
Client: send packet to server's IP/Port
Server: respond to packet from client
UDP has a number of pros and cons over TCP. I'll try to cover a few of them:
I'm sure there are a number of other Pros and cons that I am forgetting at the moment, but hopefully you get the idea.
To answer your other questions...
yes, a lot of games use UDP, and many use both UDP and TCP.
No, it's not a lot of trouble. In many cases, UDP sockets can actually be easier to implement than TCP sockets.
Re: UDP over TCP
This reduces some overhead in the IP-packets and resending and checking, which makes UDP faster, but less reliable.
UDP is often used for short data transfers, like DNS for example. Strangely enough (to my perception) it's also used for NFS though. (nowadays NFS over TCP also seem to be possible)
Well, I have looked at and worked on the source code for Vavoom (www.vavoom-engine.com), and it had two functions to send data, and unreliable one, and a reliable one. I am guessing that Vavoom either used UDP and TCP (wich seams like a waste to me), or when sending reliable, then did its own ACK/NACK.
I don't have any problem with ACK and NACK.
I am using SDL_Net, could someone please use this link to gather info, and write up a quick UDP (as in like pesado, 10 lines or less) program to open a UDP connection to a pretend server and send a packet?
This is what I am thinking:
The reason games use UDP is because they don't care about knowing whether or not the receiving end got every packet. UDP is also used in things like video and audio streaming, because a lost packet really doesn't matter. UDP really is much faster than TCP for these types of applications. But if you're going to use UDP and need to make sure the receiving end gets all of the data intact and in the right order then you'll just be re-implementing everything that you'd gain by not using TCP.
Not only do UDP packets create less overhead for the system, but they also save bandwidth. Each TCP packet you send has to receive an ACK from the receiving end. This creates extra traffic which the programs I mentioned above don't need.
The rule of thumb is: If you don't care that the receiving end gets all the packets, use UDP.
|All times are GMT -5. The time now is 10:47 PM.|