So I have a client/server game and each time a client makes a move the game outputs to the DataOutputStream and is received by a DataInputStream, the first message is a join message which tells the server the users name (this message is properly received), but all subsequent messages get skewed.
So I initialize the stream like this after connecting to server/client
private DataInputstream out = new DataOutputStream(socket.getOutputStream());
private DataInputStream in = new DataInputStream(socket.getInputStream());
The methods to send the messages to the server:
/**
* Tells server someone has joined
*/
public void join(ViewProxy proxy, String session) throws IOException {
out.writeByte('J');
out.writeUTF(session);
out.flush();
}
/**
* tells server that a player took these numbers from a heap
*
* @param x the heap
* @param y the number of items removed
* @throws IOException
*/
@Override
public void placed(int id, int x, int y) throws IOException {
out.writeByte('P');
out.writeInt(id);
out.writeInt(x);
out.writeInt(y);
out.flush();
}
/**
* Tells server to start a new game
*
* @throws IOException
*/
@Override
public void newgame() throws IOException {
out.writeByte('N');
out.flush();
}
/**
* Tells server to quit the game
*
* @throws IOException
*/
@Override
public void quit() throws IOException {
out.writeByte('Q');
out.flush();
}
Lastly how the server reads the messages:
private class ReaderThread extends Thread {
public void run() {
try {
for (;;) {
String session;
byte b = in.readByte();
switch (b)
{
case 'J':
session = in.readUTF();
System.out.println("Received J");
viewListener.join (ViewProxy.this, session);
break;
case 'P':
System.out.println("Received P");
int id = in.readInt();
int r = in.readInt();
int c = in.readInt();
viewListener.placed (id, r, c);
break;
case 'N':
System.out.println("Received N");
viewListener.newgame();
break;
case 'Q':
System.out.println("Received Q");
viewListener.quit();
break;
default:
System.err.println ("Bad message");
break;
}
}
} catch (IOException exc) {
} finally {
try {
socket.close();
} catch (IOException exc) {
}
}
}
}
When I run the server followed by the client the server receives the initial 'J' (from join method) and everything runs smoothly, but each other subsequent message I get a "Bad Message" as output meaning that the server never received any of the other messages that should have been sent through the stream.
For instance when the placed function is called with some trivial arguments, I get 13 copies of "Bad message" printed to console.
So my question here is what I am I missing? Is the DataOutputStream sending what I want I'm just interpreting it incorrectly or is something more complex going on.
After messing with the program I've removed the flush statements. I also printed what the client was sending and what the server was seeing. The client is sending the right things but the server can't seem to pick up on them. It instead sees 7 zero's (null). 1 for the letter 'P' and 6 for more for what I believe are the three integers.
Also I'm including the a link to my github for access to the full code (located in the src folder) : https://github.com/michaelrinos/Connect-Fout-Client-Server/tree/TCP
Output from the modelproxy class (client side):
Writting J and "john1" <Byte> <UTF>
Sending: 0<Byte> 0<Byte> 5<Byte>
Output from the second modelproxy class (client side):
Writting J and "john2" <Byte> <UTF>
Output from the viewproxy class (server side):
Received J and john1
Received J and john2
0
Bad message
0
Bad message
0
Bad message
5
Bad message
You Can see although the numbers are correct but I lose the leading char.