ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Aside from all ports to 1024 being usable only by root, Have a look at the IANA assigned ports. You can use any port not in the IANA list; however, anyone else can also use that port. You need to read your 'port' from a configuration file like most software does.
That is only for your 'listening' port though; all connections are dynamically assigned their own port when you accept() the connection (TCP). Obviously UDP works differently and if you don't always send to a fixed port, you will probably just lose most of the UDP packets.
Why use UDP at all? Due to the design of the internet, packets can arrive out of order, can be repeated, or simply lost (especially when you use UDP). There are libraries out there to handle these UDP issues (for example, RakNet), but if you are thinking of rewriting your own UDP code, give yourself a few months to work out most of the problems.
i was going to use UDP for positional info(i believe it is faster than TCP).
the game itself is going to be a 2D multiplayer conflict game.
the following question springs to mind:
What if i use one in the list? internet police come and get me?
Lee.
Well, UDP is exactly as fast as TCP - there's no reason the signals will go down the wires any faster. TCP only goes funny when a message isn't delivered; it will retry the transmission a number of times with an increasing time interval between retries, and if you keep sending messages, the machine at the other end may suddenly see a packet storm when all the packets are delivered. Companies like Blizzard claim that TCP works fine for online games despite the odd retry-related glitch. If you really want to use UDP, have a look at RakNet - it manages reliable packet telemetry as well as 'normal' unreliable UDP packets. Maybe there are other alternatives around as well - when I looked into this in 2005 RakNet was the only one I could find.
Lucky for you there are no internet police. You can use absolutely any port you want on your own computers. The IANA list is there for interoperability - if you use a random port and ignore IANA assignments and you try to install another service later (like http) you may find that your new service can't open a port. And let's say you set up a corporate web page - but you used a different port for http - then only people who knew the secret (or used port sniffers) could connect to your website.
Why use UDP at all? Due to the design of the internet, packets can arrive out of order, can be repeated, or simply lost (especially when you use UDP).
This is not a problem for fast paced games, information such as position velocity if dropped by the network is fine as there will be another packet along soon which will give the newest and correct information, if an old packet is received it will be dropped by the network code as the information was time sensitive.
You really want to head over to gamedev.net specifically the network forum, here is some of the answers from the FAQ's.
Quote:
Q3) Should I use TCP or UDP?
A3) There are at least four different kinds of games as far as networking is concerned: Turn-based games, Real Time Strategy games, Role Playing Games, and Action Games. Briefly, if you're turn-based, you should go with TCP because it's simpler, unless you need peer-to-peer NAT punch-through. For RTS games, you're usually best off using TCP, although UDP can make sense in extreme cases. For role playing games, the story is less clear -- action-based RPGs with lots of kinetics, like City of Heroes, use UDP, whereas slower RPGs and MUDs often stay with TCP. For action-based games like first-person shooters or racers, you should absolutely use UDP.
Q4) If TCP makes sure the data gets to me without me needing to worry about it, why would I want to use UDP, which makes no such guarantees?
A4) TCP is a stream-based protocol. What goes in one end, will come out the other end, in order, with no duplication or packet drops. This is very convenient when correctness is paramount. However, to make this guarantee, TCP must suspend all packet delivery to the receiving end, if one packet is lost, until that packet can be detected as lost and re-sent. This may cause hick-ups of several seconds for a single packet that's lost!
Q5) Is UDP faster than TCP?
A5) UDP uses slightly smaller headers per packet than TCP, which may give you marginally better throughput on narrow channels (such as modems). UDP will also deliver any received packet as it is received, without waiting to re-order packets, so if latency is more important than correctness of the underlying channel, then UDP will introduce less jitter in your game, and gameplay will be smoother.
This is not a problem for fast paced games, information such as position velocity if dropped by the network is fine as there will be another packet along soon which will give the newest and correct information, if an old packet is received it will be dropped by the network code as the information was time sensitive.
You really want to head over to gamedev.net specifically the network forum, here is some of the answers from the FAQ's.
I'm familiar with all the issues - I'm just saying that games like Blizzard's WoW use TCP and Blizzard claims it's fine except for the occasional and rare glitch. Blizzard isn't the only company making these claims and selling MMORPGs that use TCP. The other point is that you do need to spend a lot of time to get UDP working for you; games will always need a guaranteed delivered packet as well as the non-guaranteed packet. To make UDP 'reliable' involves an awful lot of work.
i was going to use UDP for positional info(i believe it is faster than TCP).
the game itself is going to be a 2D multiplayer conflict game.
Lee.
Your strategy to use UDP for this is valid. If you are updating things rapidly, especially where the entire atom of data can be encapsulated in a single datagram, you don't necessarily car about a few dropped packets. Your most important information is likely be the most recent data only. For motion oriented data, that would mean you probably need to know where an object is now, and where is was at any time in the past is less important; maybe completely unimportant.
Other aspects of the data may require reliability, to ensure that completeness and order-of-events is maintained.
--- rod.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.