So I have a server-client combo which is just supposed to pass a custom Object back and forth. I’ using the ObjectInputStream and ObjectOutpustStream classes to achieve this.
Here is the server’s loop:
while((inputPosition = (Vector2i) objectIn.readObject()) != null) {
Print.log("input line: " + inputPosition.toString());
outputLine = "you moved to " + inputPosition.toString();
out.println(outputLine);
}
Where inputPosition is a Vector2i, a simple class that just holds 2 integers, x and y.
Here is the client’s loop:
while((serverOutput = in.readLine()) != null) {
Print.log("Server says: " + serverOutput);
position = calculatePosition(position, reader);
Print.log("sending over: " + position.toString());
objectOut.writeObject(position);
}
The calculate position method just looks like this:
private static Vector2i calculatePosition(Vector2i position, BufferedReader reader) throws IOException {
Print.log("i just got this: " + position.toString());
String entry = reader.readLine().substring(0, 1);
if(entry.equals("w"))
position.y++;
else if(entry.equals("s"))
position.y--;
else if(entry.equals("a"))
position.x--;
else if(entry.equals("d"))
position.x++;
return position;
}
So here is what happens. I connect to the server, and after moving one coordinate successfully, it just get’s stuck on the same coordinate over and over:
Server says: Use wasd to move around.
i just got this: 5, 5
w
sending over: 5, 6
Server says: you moved to 5, 6
i just got this: 5, 6
w
sending over: 5, 7
Server says: you moved to 5, 6
i just got this: 5, 7
a
sending over: 4, 7
Server says: you moved to 5, 6
i just got this: 4, 7
You can see in the “sending over” line that the vector2i object on the client side is up-to-date, but the response I get from the server is just the same one over and over. There server’s log looks like this:
input line: 5, 6
input line: 5, 6
input line: 5, 6
It seems to be receiving the same data over and over, but according to my log, the client should be sending new data over.
Does anyone have an idea what I did wrong?
After you have sent an object once, it send a reference to that object. This means
The way to avoid both these issue is to call
reset()regularly. The library can’t do this for you as it doesn’t know what it can be safely done.