Network game communication algorithms... small question

Just a (hopefully) quick question about getting input from the client side for passing information to a server. At the moment it is set up to take and send the input from the client once the return key is pressed, and send the whole line (with the system.in part). But obviously for a game to be played I am needing the client to send every individual key press from the client. I could try and use the read() method for the bufferedreader, but this returns an int, and I require a string so it can be checked in the server (as I am also going to have a server side representation of the game, as the project is to be investigating lag effects and different solutions to get around online game lag). Or could I use the read() method and use the returned int to find out from that key what was pressed and then change it to the key pressed from the int value returned, or is there a quicker way that doing this check?

I think there was a method that takes a character array as a parameter and I could somehow set the size to be only 1, so it would send every individual key pressed by the client, then allowing me to send this and display it at the server side? Or am I off on the wrong foot entirely...

This is the basic code of getting the actual input and then calling the methods which actually sort out writing to the streams elsewhere in the program:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

...

s = br.readLine();

if (s.length() > 0)

cc.sendMessage(s + "\n");

Thanks for any help given,

Cheers

[1598 byte] By [Podgea] at [2007-10-2 3:53:10]
# 1

Hi,

You could send the KeyEvent object created by the key press event using an ObjectOutputStream or you could just write its code (KeyEvent.getKeyCode()) using DataOutputStream.writeInt().

If you need the distinction between a press and a release you could write presses as normal key codes and releases as negative key codes.

In a game I'm making that uses UDP instead of TCP to communicate over the network, since UDP packets aren't guaranteed to get to their destination, whenever a key is pressed or released I send a whole array of integer keyCodes that show which keys are down. This way when a key is released, but the packet indicating the release doesn't get to the server, the server will find out eventually when the next key is pressed/released and a new 'key state' array is sent.

CommanderKeitha at 2007-7-15 23:09:40 > top of Java-index,Other Topics,Java Game Development...