Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  UDP and TCP  (Read 797 times)
0 Members and 1 Guest are viewing this topic.
Offline Yemto

Junior Member


Exp: 3 years



« Posted 2013-09-09 17:23:57 »

So I was thinking of beginning to develop a multiplayer game. I have done it before, so it's not the code I need help with. It's UDP and TCP. My idea is to use both.

TCP, would take care of player commands, and important information. Such as; logging in, logging out, chat, and commands. With "commands" I mean things like order a unit to move, attack, defend, collect and so on.

UDP, would send where all the units are, updating the map, upgrades, and unit health.

For example:
Player 1 uses an ability to send a meteor strike. (TCP)
The server sends the meteor's; Coordinates, velocity, and direction to all clients. (UDP, since it will be updated quite often.)
The meteor hit the ground, and the server send the terrain deformation to all clients (UDP, the map is updated regularly)
Player 2 get's angry since he gets hit, and leaves the game (TCP)

Is that a good way to use TCP and UDP? or am I missing something?
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #1 - Posted 2013-09-09 17:31:26 »

There's basically three ways to approach it:

1. Use tcp everywhere and just put up with the (potential) extra latency added.
2. Use udp everywhere and write your own transmission protocol to guarantee delivery when required.
3. Use both at the same time.

I've previously done all three, and they all basically work. Option 3 is nicer if you want to avoid writing a reliability layer over udp, but can make the initial connecting and handshaking more of a pain since you've got two connections to manage instead of one. What you've described is completely valid if that's the way you want to go.

If you're only concerned with minimising latency, then 2 can be good as you can theoretically get a faster reliable protocol than TCP if you bend some of the rules TCP adheres to.

However if this is your first multiplayer game, or if you're just trying out some ideas, totally go for 1. You can focus on your mechanics and gameplay *much* quicker, and you probably won't notice the difference in latency unless you've got players in different countries or across mobile networks. I'm doing 1 right now to prove some gameplay and latency is completely not an issue.

Also, consider Kryonet: https://code.google.com/p/kryonet/

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Yemto

Junior Member


Exp: 3 years



« Reply #2 - Posted 2013-09-09 17:40:45 »

There's basically three ways to approach it:

1. Use tcp everywhere and just put up with the (potential) extra latency added.
2. Use udp everywhere and write your own transmission protocol to guarantee delivery when required.
3. Use both at the same time.

I've previously done all three, and they all basically work. Option 3 is nicer if you want to avoid writing a reliability layer over udp, but can make the initial connecting and handshaking more of a pain since you've got two connections to manage instead of one. What you've described is completely valid if that's the way you want to go.

If you're only concerned with minimising latency, then 2 can be good as you can theoretically get a faster reliable protocol than TCP if you bend some of the rules TCP adheres to.

However if this is your first multiplayer game, or if you're just trying out some ideas, totally go for 1. You can focus on your mechanics and gameplay *much* quicker, and you probably won't notice the difference in latency unless you've got players in different countries or across mobile networks. I'm doing 1 right now to prove some gameplay and latency is completely not an issue.

This would be my 2nd multiplayer game, I did one sometime ago that only used TCP, and I'm just trying to get lower latency. The map can get deformed, and all the units coordinates needs to be updated quite often (I think). I would go for all UDP, since I like it more. But I have no experience in coding UDP, and what to do when it loses data.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2013-09-09 17:47:32 »

I've made a multiplayer game using both UDP and TCP and it works very nicely (as long as you don't saturate the TCP connection, but use it sparingly for data that must be sent reliably).

Offline jonjava
« Reply #4 - Posted 2013-09-09 19:09:20 »

Read these:

http://gafferongames.com/networking-for-game-programmers/udp-vs-tcp/
http://gafferongames.com/networking-for-game-programmers/sending-and-receiving-packets/
http://gafferongames.com/networking-for-game-programmers/virtual-connection-over-udp/


In fact, just read all of the connected articles - then make your choice based on what your game needs. The suggestions in this thread are all valid.

Offline Several Kilo-Bytes

Senior Member


Medals: 11



« Reply #5 - Posted 2013-09-09 23:28:28 »

I think there is a problem with with using TCP and UDP where using TCP delays or causes UDP packets to drop more frequently or something. If someone knows what that reason is I would also like to know.

Also, if you have the infrastructure to use acks in your UDP game what benefit does TCP give you?
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2013-09-10 06:09:23 »

I think there is a problem with with using TCP and UDP where using TCP delays or causes UDP packets to drop more frequently or something. If someone knows what that reason is I would also like to know.
It doesn't. UDP packets are only dropped when there is no space in the buffers to store them. Having an extra connection that you only use sparingly for important data will barely affect the UDP packets.

Offline jonjava
« Reply #7 - Posted 2013-09-10 08:28:47 »

I think there is a problem with with using TCP and UDP where using TCP delays or causes UDP packets to drop more frequently or something. If someone knows what that reason is I would also like to know.

Also, if you have the infrastructure to use acks in your UDP game what benefit does TCP give you?

[Characteristics of UDP Packet Loss: Effect of TCP Traffic]
http://www.isoc.org/INET97/proceedings/F3/F3_1.HTM

Offline jonjava
« Reply #8 - Posted 2013-09-10 08:36:01 »

It doesn't. UDP packets are only dropped when there is no space in the buffers to store them. Having an extra connection that you only use sparingly for important data will barely affect the UDP packets.

I haven't had noticeable troubles myself with this and it probably won't effect the majority of small games but an expert once said this:


source: http://gafferongames.com/networking-for-game-programmers/udp-vs-tcp/

Offline Jeremy
« Reply #9 - Posted 2013-09-10 09:05:51 »

Overall, choosing one or the other comes down I think to how much client-side prediction you can do.

In an FPS, you can't do much. You receive a velocity and acceleration that is probably valid for 200ms until the user presses some other key to accelerate themselves in another direction. Further, in an FPS if the order of these packets are mixed up, you haven't lost much. If one of them are dropped, no one is going to notice (and they aren't worth resending)

In an MMO (i.e, WoW uses A LOT of TCP, so do most other MMOs) you can do a lot of client-side prediction. Attacking an enemy for example is very straight forward. You initiate an attack, and you end an attack. There is nothing that can be dropped in a game like WoW or in JevaEngine that wouldn't have to be resent anyways.

To conclude, if you implement networking properly it won't be difficult to go from one to the other, so I'd say start with TCP and move to UDP if you need it.

Don't drop TCP just because it doesn't work the first time, you can try manually disabling Nagle's algorithm to prevent message queuing as well (which helps reduce packet overhead if you are sending small packets - but it may help optimize your game quite a bit if you need instant transmission of packets) Careful configuration of TCP sockets can go a long way.

http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#setTcpNoDelay%28boolean%29

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2013-09-10 21:13:00 »

Experienced friends don't let rookie friends write a reliable layer on top of UDP.

TL;DR
go with TCP, make it work, ponder your options, stick with TCP.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline jonjava
« Reply #11 - Posted 2013-09-11 09:40:15 »

Well he says he's done it before and doesn't need help with the code :V

But you're right. TCP is easier to use and it's 99%* of the time just fine, perfect even, for our needs.

*This estimate is 96.33(3)% accurate.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (32 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!