Sign in to follow this  
MrProper

Communication between clients

Recommended Posts

Hi,

I am programming simple MP game in java, but I am little confused about multiplayer part. Problem is, I cant find a way to make clients connected to server communicate with each other.

I am creating new thread for each connection, but still I fail to see how to send information from client1 to server and from server to client2. I know there must be some simple solution, but it just seems missing me.

Share this post


Link to post
Share on other sites
ok, so i have managed to create something, its working, but i am not completly sure about it...

Im giving to every connection object, where are stored references for its
output stream and also output stream of other player. Is it good idea? Are there any possible drawbacks of this solution?

Share this post


Link to post
Share on other sites
Generally in most cases each client will provide updates about it's local objects player position, chat input etc. The server then compiles all the inputs from each of the clients into one frame update. This is then sent on to all the clients whereupon they update their versions of the objects with the servers data.

Connections between clients are generally not used in the standard client-server structure but are used in peer to peer structures.

Share this post


Link to post
Share on other sites
To illustrate Darg's point, your server likely has a number of classes that are handling various aspects of the game server's logic.

Connection Handler:

public void handleMovementPacket(WorldPacket packet) {
gridManager.movePlayer(getClientConnection(), packet);
}

Essentially the above illustrates that the connection handler has a method specifically for movement packets. When the packet is determined to be a movement type, this method is called and it invokes the grid manager to manipulate the player's location.

Grid Manager

public void movePlayer(ClientConnection client, WorldPacket packet) {
GridPosition pos = getPosition(client);
if(packet.getType().equals(MOVE_FORWARD)) {
pos.x += FORWARD_STEP_VALUE;
updatePosition(client, pos);
ClientConnection[] clients = getNearbyClients(pos);
SendUpdateToClients(packet, clients);
}
}
public void SendUpdateToClients(WorldPacket packet, ClientConnection[] c) {
for(ClientConnection client : c) {
SendUpdateToClient(packet, c);
}
}
public void SendUpdateToClient(WorldPacket packet, ClientConnection c) {
// either queue or directly send
c.sendPacket(packet);
}

hope this helps

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this