Tukhes
Tukhes

Reputation: 71

Java Multiplayer Game Networking Concept

I'm developing a multiplayer turn based strategy game in Java (both client and server). I don't have much experience in networked games but I have done small things like a multiclient chat and 2 player tic tac toe with sockets. I'm not looking for someone to code this for me but just give me some guidance.

The game goes through multiple stages: first a user connects and logins. After he is given the option to host/join a game. After he joins a game or someone joins his game, the client moves on to the game stage. The game is 1v1 and only needs to send data back and forth every 5 seconds (if that's important?). I just need some guidance on how you could design a client/server to move through these "stages".

This is how I'm currently thinking of implementing it:

  1. When a connection is made the connection will have a corresponding variable that describes state on the server. For example: 0 is before login, 1 is after login, 2 is hosting, etc...

  2. Whenever the client sends data to the server it checks for the state and deals with it accordingly. Like before login if data is sent, the server will assume it's login details and if they're valid it changes the variable to 1 and tells the client to advance.

Is there a better way to do this?

Upvotes: 1

Views: 4772

Answers (3)

kingston
kingston

Reputation: 1

You can use Mina or Netty as Nio socket framework. Using Java both in server and client makes it easier to design message protocol. When one client moves, you send a moving message to the server, and then server pushes back a message to both clients. Both clients move together when receiving the message. Thus two clients move synchronously if avoiding network delay. You can take a look at the open-source game framework written in java.

java game server engine

Upvotes: 0

Alex DiCarlo
Alex DiCarlo

Reputation: 4891

Essentially how you described it is how it is commonly done. Simply have a server listening for client connections, and then deal with client connections in its own thread as they come up. Then send messages back and forth to confirm state (logged in vs. logged out, joining game, etc.) and messages while in game for player moves. Either a TCP or UDP socket will work in the short term, however eventually you will probably switch to a primarily UDP based system as the messages sent between server and client will be fairly small and latency could be key depending on the game type.

Consider sending/recieving messages via JSON (http://wiki.fasterxml.com/JacksonHome is a fairly popular Java parser last I checked).

Additionally, you'll need several data structures on the server side to efficiently manage clients and their states. For example, you could have an integer id for each client and a Map<Integer, Client> that you would use to store all currently connected logged in users, where Client is some object that stores the current state of that user.

Hopefully this gives you some ideas on where to start.

Upvotes: 2

Adri&#225;n
Adri&#225;n

Reputation: 6255

Since is Full Java + Client-Server + Game:

I recommend you to forget using straight sockets.

In my opinion Java RMI is your best bet.

Why?:

  • Easier than sockets once you have it learned.

  • You can call remote methods straight from your java client/server.

  • You can use callbacks. (i.e. Server testing that the clients are still connected)

There are probably even more reasons...

Upvotes: 2

Related Questions