Sending Keystroke across network

Started by
11 comments, last by oliii 11 years, 4 months ago
I am aware that it makes sense to do some preprocessing of the input on the clients. When talking about sending key states I was thinking of a bitmask of actions as mentioned by radioteeth in the first response. And that coordinates in a user command in a RTS game are in world coordinates also totally makes sense. I would also consider sending a direction vector instead of mouse movement information as this kind of preprocessing.


But the OP was asking about moving a character with the keyboard and not about hit detection in a FPS. My point was, that sending a bitmask of key or action states, for each simulation frame can work perfectly fine. You just have to make sure that the user commands arrive in time most of the time.
Advertisement
Hey thanks for the replies, I should probably say I am only doing an ascii console game using the SocketLib and ThreadLib libraries from Ron Penton's MUD programming so I assume my choices are limited here. It seems I am only allowed to send data as char* according to the send function within WinSock2.h, so I am not sure if this makes a difference? How can I send bits instead of chars? Is it possible to send custom structures using WinSock?

I have posted a related question to this with regards to threading, I wanted it to be separate from this as it may or may not be directly related.
You need to look into 'serialisation'.

What serialisation does is convert your object properties back and forth into byte streams / bitstreams, or a byte array.

It is similar to writing or reading your data into a binary file, or even a screen console output, if you are more familiar with that.

If you use UDP, then you can wrap a char array with a binary stream handler to convert your objects to and from binary.

Look up boost serialization, if you want to delve deeper.

But really, if you want the short answer, you would do something like this



//--------------------------------
// player definition.
//--------------------------------
struct Player
{
float px;
float py;
float vx;
float vy;
u32 actions;
};


//--------------------------------
// CLIENT
//--------------------------------
Player player;
sendto(socket_handle, &player, sizeof(player), 0, &server_address, sizeof(server_address));

//--------------------------------
// SERVER
//--------------------------------
Player player;
recvfrom(socket_handle, &player, sizeof(player), 0, &client_address, &client_address_len);


Note that I strongly suggest you do not use that method and do proper serialization, or at the very least understand the many problems associated with the code above.

Everything is better with Metal.

This topic is closed to new replies.

Advertisement