1
votes

I am making a real-time java game for my friends to play on LAN. The game is working in a client-server architecture, and it is using UDP for everything right now (for both position updates and joining to the game). When I tested the game with my friend over the Internet, a few important UDP packets were lost (like the one that is for spawning enemy).

So my question is, what is the best solution for making real-time multiplayer games, work? Can I use UDP for the necesarry update packets and TCP for packets like log-in, disconnect, chat etc. ? Can i use both protocol on the same port and socket?

3
If you need guaranteed delivery or guaranteed order of delivery, UDP is not for you unless you are prepared to implement those function in your software. UDP is a send-and-forget, best-effort protocol. TCP guarantees delivery and delivery order.Ron Maupin
The thing is I need both of them. Guaranteed delivery for packets when a new player log in to the Server, and send-and-forget packets for movement and RT stuff. Can I implement like an ask-and-resend system with UDP, or would it be worth to do? Or should I // Can I just use TCPHyderox
That is up to you. You could implement your own ask-and-resend for UDP, or just use TCP for both. You know your requirements. You could use two different TCP ports for the two separate functions, or you could use the same port number for both UDP and TCP. Weigh you options...Ron Maupin

3 Answers

0
votes

you can combine both.Sometimes you may want reliable data ,that time you need TCp.Sometimes its not required so that time u can use UDP.Combine the both.

1
votes

Just want to help any passers by that stumble on the incredible bad advice given in the other answers. Sorry, I am sure it is well meaning. Do NOT use TCP in games if you have any kind of realtime aspect to it. You will regret it at some point down the road.

What you want is to implement reliability/connections over UDP. Gaffer on Games have an excellent series on this topic. I cordially recommend everyone interested in this topic to read the whole series. Here's a link to the post describing "connections" over UDP: https://gafferongames.com/post/virtual_connection_over_udp/

And you're in luck - they even implemented all of this in an open source library <3 https://github.com/networkprotocol/yojimbo

0
votes

Your best bet is just to use TCP. In addition to providing the resend capabilities for you, so you won't have to write them yourself, along with a host off other good features, TCP will also make it a lot easier to deal with routers and firewalls.

I do recommend turning off the Nagle algorithm so data is always sent immediately.