Basic Game Networking Structure - Sending Objects..?

Hey everyone,

I have a really bare bones networking question. Right now I have set up a multithreaded chat server, which a Client can connect to and chat with other clients connected to it, however I want to expand it.

I want the client to be a basic map where the user can move a ball (character). So what I am thinking I can do is just send over the coordinates of the client to the server , and have it mass send it back to all clients and in the client have an arraylist of positions and use a for loop to go through them and paint a ball at those coordinates.

However the problem I am having is, how would I get the server to know Client1 (one of the clients connected) has updated his position and to change it. Im guessing I would need some type of ID for each of the clients that would be registered when they connect to the server. But im not sure how i would do that.

Would I use an ArrayList, or hashmap or something so i can have 2 values for each element (“ClientA”, Point p), or should I just have the Server send the ArrayList of Character Objects over the network and just have the clients draw them?

Sorry if it doesnt make too much sense, im half asking a question half rambling ideas.

instead of sending the new position every game cycle, instead just try sending the players keyboard requests as the are pressed/unpressed

so while the keyboard up arrow, is pressed. Send data to all players saying that player# has keypressed, then send the update when the keyboard is lifted, its ALOT less data, then sending position updates every cycle. All clients should calcualte the changes the same.

Ok so the client will receive the information that Player1, has pressed up, and then the Client will update that players position?

One thing I dont understand though, is how to assign Players ID and how to tell the Client player1 has pressed up, player2 has pressed down.

Would every client just have an ID, and then when the player1 sends that it has pressed up it would send like playerID+action, and then the server will send that back to everyone? but when it sends it back to everyone, how do the clients know who to update? Do we have to store all the players in an ArrayList or hashmap in the client or the server? (probably both right?)

That sounds like it might work in a fantasy world? What if something is depending on randomness? I dont think it is EVER a good idea to count on clients to do the same calculations. Also, you should think alot about what kind of application you are creating. Is sercurity a top priority? If so it would be nice if the server did almost all the calculations, so that clients wouldnt be able to manipulate their information for benefits. I’m not very experienced with networking yet, but I think alot of thought should go into the decisiont you make about server/client-side calculations.

My solution would be for the clients to send their updated coordinates and ask for server-side coordinates frequently(maybe not EVERY cycle).

Hi !
For a game or even a chat must be clearly updated very frequently, it’s worth to have a server-side application that handles the WHOLE AI AND CHAT system unlike the client would neither use the AI or CHAT as an algortihm. In other words in networking systems, the server always takes place of the original algorithm as if locally run.
Then for the question above in the thread, clients must ONLY send their INPUTS and the server COMPUTES new STATES OR POSITIONS before to FEED BACK THEM to the clients, so in a way that the CLI-Application can UPDATE GRAPHICS ONLY.
See : CLI-SERV interface {SEND INPUT/Keyboard, REC STATE/POS} , SERV-CLI interface {SEND STATE/POS, REC INPUT/Keyboard} , SERVER algorithm {COMPUTE, ARRAY-LIST cache, NOTIFY UPDATE} and CLIENT {LISTEN KEYBOARD, RESUME on UPDATE, DISPLAY CHAT/PLAYERS}
For a peer-to-peer protocol, either one PC would be acting as a Server and the other as a Client. This is the fastest way, I’m sure. ::slight_smile:

I’m guessing you are right in this example, but can someone tell me what would happen if the logic was very CPU-demanding, then we couldnt rely on the server to do physics for thousands of players on a giant multiplayer-server?

you said that it was a multi-threaded server app. So just send the array index to the client, so the client knows what index it is.

Well yes, It is multithreaded, and I have each of the serverthreads (connections) stored in an arraylist, but how do i get that arraylist to the client?

for example, lets say a user connects, in the server side he is added to the connections ArrayList, but what about the client? Does the client have an arraylist of connections also? Thats why I was wondering how i could send an arraylist over a network…that way when a user connects, it will send all other users the updated arraylist of players…does that make sense?

**Also i see in the other thread about making something serializable (objects I believe) and then just sending it to the Server, have the client do what it needs, and just have the client take commands (key input, mouse clicks) and draw.

I would recommend not sending objects, (other than UTF).
just get the server to cycle through an array and write the data, and have the client on the other end, cycle through the read array. You would need to send the array size first.

I handle my network request by assigning a byte to the type of request that is going to be sent.
so if you are going to transfer a playerList, send a byte called PLAYER_LIST, the on the client side, get ready to read another int, for size of an array, then read the players name Strings(as UTFs).

If you still havnt implemented a way of sending info to ther connection threads, you will need to add a function that takes data to send,

The way i handle it is by adding a function in the connection thread called. addRequest, that takes a Request Object.

But what i dont get…Is it an ArrayList of type, Player or would it just be an array of Points? And would it be like this
Client connects to server
Server adds a new Player Object to the ArrayList
Server sends a message to all clients (through a for loop) for example

for(int i = 0; i < players.size(); i++)
{
Player p = players.get(i);
out.writeUTF(i+“PosX”+p.getX();
etc etc
}
then in the client read in this information by
splitting up the message, the first letter is the index of the array (the current character)
the second part is what its updating, and the third what the update is.
and if the part of the array is null, you just create a new player with that information?

I ment write UTF for player Names, I ment that as an exception to the rule, of not sending objects
but as for positions, just send them as the variable, writeFloat/writeInt…

then on the client side just have something like:

for a single player position update…

int currentIndex = in.readInt();
player[currentIndex].setXPos(in.readFloat());
player[currentIndex].setYPos(in.readFloat());

for every player position update…

for (int i = 0; i < player.length; i++) {
player[currentIndex].setXPos(in.readFloat());
player[currentIndex].setYPos(in.readFloat());
}

I really dont understand what the problem is?

what current aplications have you made with your multi-threaded server, any turn based games?

and how are you now trying to upgrade it, from what it is?

Well basically what i have is a Multithreaded client server chat program. A server runs, accepts clients, and when a client sends a message, it sends it to the server which sends it to all other clients and thats it. The problem is adding essentially Players, from a player class…but im guessing everytime the user presses up, the client will send
clientID+“UP”
where clientID is the index in the array which is server side, then the server will send that message to all clients and all clients will update
clientID’s y position by w.e value?

I personally think thats the best way to do it. If you do decide to work with client side calculation, I recommend, define the class as strictfp, probably not important for what your doing, but its just a simple keyword to knock off, and prevent any relating future problems relating to the float value precision.

Ahhh man thank you very much!! got a basic thing up and running!!
the characters can walk around, and chat…

although a problem comes in when updating movement, for example if i hold down right which sends A LOT of messages to the server telling it i pushed right (because while right is pusehd it sends the message, i think what is should do is just send it pushed right, and released right) which updates the characters movement for every one of those messages so that even when i let go its still updating and moving. Also 2 characters cannot move at the same time, they are jittery for example i would move, he will move, etc, etc.

yeah, the message shouldnt send every cycle. only for key down, and key up.

there should be a state varible for the character, something like a boolean isMovingRight, and set to true when key down press is on.
then when updating the game cycle just check the boolean state.

Another thing to make sure the client is in sync to the server: when you send a move request, only after the server accepts that request, forward that request back to all the clients, that way even the client that sent the request needs to wait for the server. All clients will be in sync this way.

So even though there are client side calculations the server should decide weather or not a request passes.

Best to store in a HashMap, as you can only assign 1 unique ID to each player. If you want perma ids, use MySQL or NaviCat. If you want temporary IDs, use a static int in your server sided code. The server tells the client what ID it is assigned.