Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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 1453 times)
0 Members and 1 Guest are viewing this topic.
Offline appel

JGO Wizard


Medals: 51
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 @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline sunsett

Senior Duke




ribbit!


« 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 (http://javagamenetworking.dev.java.net) 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: 51
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) {
   mplay.sendMyInput(myPlayer);

   update();
   render();

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



How am I doing so far?

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline appel

JGO Wizard


Medals: 51
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 @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline sunsett

Senior Duke




ribbit!


« 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.

 

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

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (67 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!