4 Replies - 783 Views - Last Post: 09 July 2013 - 11:47 AM Rate Topic: -----

#1 JTG2003  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 29-February 12

Network Model for Client/Server Gaming

Posted 09 July 2013 - 10:38 AM

Preface:
- Years ago I created a FTP client in C using TCP. Nothing fancy, only used 1 client and a server.
- Read through the networking section in the official Java Tutorials.
- Did a search for "network" in "Java" and "Game Development" forums, reviewed 4 pages of results, some of which were helpful.
Two resources that look like they could be of some help

Java TCP Chat Program

Simple UDP Client/Server

I have created a little game, nothing much at the moment, with a player and a map. Before I get too far, I want to incorporate networking because I feel like it would be easier to have networking in mind from the start rather than try to add it later.

Currently, I have a server that will listen on a port for a connection and the client (with a prompt for an IP) that can connect to it.

This isn't necessarily a Java specific question, but it might be.

Basically I'm trying to figure out a good network model for client/server interaction.

Model 1:
Client: Player presses "Up" key
Client: Send "Move" request to server
Client: Perform local collision to see if the player can move up, move up if possible.
Client: Allows player to continue entering input.

Server: Receive "Move" request.
Server: Checks collision, there is an object and the player cannot move there.
Server: Sends "Move" response to client.

Client: Receives "Move" response from server. Updates current position to sync with server position if it is not already there (In theory, the collision client side should be identical to the collision server side, so nothing should need to update)




Some thoughts about this. The client will need to constantly be checking for server responses, not WAITING for a response. The main game loop will consist of
While(GameRunning)
{
    if(serverMessage())
        processMessage()
    else if(playerInput())
        processInput()
}



Obviously not that simplified and with proper code, but the point here is more conceptual than code-specific.

The Good:
If there is no lag, the player will have a smooth experience.

The Bad:
The player could theoretically send multiple move requests before the server responds. The player could move 5 times, then the server could respond with "Oh, by the way, that first move you did was invalid as you actually collided with something".. then the player is 'teleported' backwards several spaces.



Model 2:
Client: Player presses "Up" key
Client: Send "Move" request to server
Client: Perform local collision to see if the player can move up, move up if possible.
Client: Waits for server response.

Server: Gets "Move" request.
Server: Checks collision, sends "OK" or "No" back to client.

Client: Gets "OK" and continues as normal or gets "No" and goes back a spot



Here, the client's main game loop will not need to be split into checking for server messages and player input as the game will actually wait.

The good:
The player will never jump back more than 1 space

The bad:
It concerns me that the player will need to wait for a server response on every move, even if it is initially checked locally. I feel like this could EASILY cause some problems.


So I guess the focus is more on the client. Is it more beneficial to have an asynchronous client where it will process server responses just like a server processes requests or a synchronous client where it will wait for a response before going ahead with more input?

I am also well aware there could be other ways to do this, so if you know of a better way, please enlighten me.

Thanks,
Jeremy

Is This A Good Question/Topic? 0
  • +

Replies To: Network Model for Client/Server Gaming

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1571
  • View blog
  • Posts: 3,529
  • Joined: 05-April 11

Re: Network Model for Client/Server Gaming

Posted 09 July 2013 - 10:57 AM

I am no expert at this area, and I have never created a network game
The topic might fit in the Game Development forum :)

You can prevent all of this in the server's game loop
The player won't necessarily be moved as soon as the move request is received

Client: Send "Move" request to server
Client: Send "Move" request to server

Server: Server wakes up from sleeping in the game loop
Server: Get all inputs from players. Only the last "Move" request should count
Server: Update locations and check for collision
Server: Send back the new locations
Server: Sleeps to add a delay in the game loop
Was This Post Helpful? 0
  • +
  • -

#3 JTG2003  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 29-February 12

Re: Network Model for Client/Server Gaming

Posted 09 July 2013 - 11:09 AM

Oh, yeah I like that! So the player may jump, but ideally never more than a couple moves worth.

I guess method 1 would HAVE to be used since the server may need to send data to the client that wasn't specifically requested.

In hindsight, this should have been in the game development forum instead, I started this thinking I'd be asking more java specific questions.

Does anyone else have any insights on this?

Thanks,
Jeremy
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13495
  • View blog
  • Posts: 53,911
  • Joined: 12-June 08

Re: Network Model for Client/Server Gaming

Posted 09 July 2013 - 11:10 AM

Moving to game dev... you may notice a few other posts along the similar lines of networking and a game.
Was This Post Helpful? 0
  • +
  • -

#5 JTG2003  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 29-February 12

Re: Network Model for Client/Server Gaming

Posted 09 July 2013 - 11:47 AM

@modi123_1: Thank you

I have a more Java-specific question about this now though ...

I see I can create the UDP socket using a DatagramSocket object. However, the "receive" function will cause the program to wait until a network message has been received. In order to have the client flowing properly, the client will need to be checking for network messages concurrently. Is this where I should look into implementing another thread for the client so that it may listen for network messages while performing the game functions?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1