Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (593)
Games in Android Showcase (168)
games submitted by our members
Games in WIP (647)
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  
  Mixing UDP and TCP?  (Read 1624 times)
0 Members and 1 Guest are viewing this topic.
Offline appel

JGO Wizard

Medals: 69
Projects: 4

I always win!

« Posted 2006-03-09 19:03:51 »

I was thinking about mixing UDP and TCP in my game.

My game is a very simple game, you're piloting a tank in field that you look down onto (top-down). You use ASDW and mouse to move around and fire at other tanks (other players).

I'm thinking about using UDP to send player input information to a dedicated-server, and let it calculate the game state of all players, and then the server sends back via UDP the new state of all players.

TCP I was going to use to do client/server formal procedures, like get game world info; log-on to server, send to server player name, check if server is full, get score of players.

Is that the right way to do it? UDP for frequent updates, TCP for infrequent but essential updates?

Check out the 4K competition @
Check out GAMADU (my own site) @
Offline sunsett

Senior Devvie


« Reply #1 - Posted 2006-03-09 19:54:15 »

In most cases you could simply use TCP for everything and it wouldn't matter much.  For faster paced games when lag can be an issue UDP is often a good choice for sending information that does not necessarily need to be in sequence or even need to be guaranteed to arrive at all.

For example, you wouldn't want to send UDP messages like "move forward 5 squares" because if that for some reason got hit out of sequence or never arrived at all, that client will then forever be out of sync.  UDP messages should contain enough information to place the object correctly at any given point without the requirement of previous information like, "put the object at position 100, 200".

There are some good APIs out there if you are interested in not re-inventing the wheel.  I'm particularly persuaded to JGN ( but I'm biased since I wrote it.  There are several others around here that have great features as well.

-Matt Hicks
Offline appel

JGO Wizard

Medals: 69
Projects: 4

I always win!

« Reply #2 - Posted 2006-03-09 21:26:32 »

Yes, I really would like some good library to use both on the client and server side.

What I really need is a good architecture for my game, currently I'm thinking about doing it something like this:

Class MPlay {
TCPClient tcp;
UDPClient udp;

int getNumberOfPlayers() // TCP, in order to show how many are playing
int getMaxNumberOfPlayers() // TCP, in order to see how many slots are available
boolean signIn(Player player) // TCP, takes up a slot for the player and registers him as a player on the server.
void sendMyInput(Player player) // UDP, sends player input.
GameState getGameState() // UDP, received from server the players state.


So, in the game loop I would have something like this:

while(true) {


   gameState = mplay.getGameState();
   enemies = gameState.getEnemies();
   myPlayer = gameState.getMyPlayer();

How am I doing so far?

Check out the 4K competition @
Check out GAMADU (my own site) @
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline appel

JGO Wizard

Medals: 69
Projects: 4

I always win!

« Reply #3 - Posted 2006-03-09 22:22:41 »

Also, I'm thinking... is it a good idea to have the client run in a seperate thread, so it can just send to server and receive from server without doing it in the game-loop?

So, having two game-loops is a good idea? One displaying the graphics, and one doing the multiplayer connectivity?

Check out the 4K competition @
Check out GAMADU (my own site) @
Offline sunsett

Senior Devvie


« Reply #4 - Posted 2006-03-10 14:22:33 »

Well, as for your example code for what you're thinking of doing you really might want to look at JGN since that's pretty close to what it does except you create Message objects that are simply beans to communicate instead of worrying about the intricacies of converting everything into a byte array.

As for the choice of having the networking on its own thread there are good arguments for both ways.  I have games that do both ways but the networking aspects will kick off to something else typically anyway, so there's not usually any real time spent in the networking aspects.

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

Riven (3 views)
2015-07-03 11:22:16

deepthought (40 views)
2015-06-30 15:39:44

deepthought (44 views)
2015-06-30 15:39:09

deepthought (54 views)
2015-06-30 15:36:52

Za\'Anzabar (24 views)
2015-06-29 05:44:54

TritonDreyja (41 views)
2015-06-24 17:10:40

CopyableCougar4 (37 views)
2015-06-23 00:34:45

BurntPizza (43 views)
2015-06-21 20:36:46

cookiecompiler (85 views)
2015-06-11 15:42:53

cookiecompiler (48 views)
2015-06-11 15:41:14
How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14 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‑
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!