CodeManu
Member
Hi all!
After been struggling with this for about 30 hours by now I've decided to post it here because I don't think I can find an explanation by myself.
Introduction: I've been working on a Java server for a GM:S game, the connection is made through TCP. The Java server connects with GM:S without problem and start sending packets of 19 bytes. The packets are received by the GM:S client but sometimes the packet content is mixed with other packets. I know TCP is streamed so I write all the data received into a buffer and then when that buffer is 19 bytes long (at least) I extract the packet from there.
The problem: From the Java server I'm sending: u16 + byte + byte + byte + u16 + u16 + u16 + u16 + u16 + u16 + u16. So it's: 2+1+1+1+2+2+2+2+2+2+2 = 19bytes. When I read the data I'm receiving the 19 bytes packet should look like this: "25-4033380808080808080", but this packet is sometimes mixed as shown in the following image:
I've highlighted in green the packets that are received fine, but you can see that the "white" packets are mixed.
The data is collected with the following code to avoid any changes from the data received. (This code runs in the Networking event)
buff = ds_map_find_value(async_load,"buffer");
buffer_seek(buff, buffer_seek_start, 0);
repeat(buffer_get_size(buff)) {
file_text_write_real(f,buffer_read(buff, buffer_s8));
}
file_text_writeln(f);
And if it helps, the data is sent from the Java server with the following code. (I know the code is ugly, I just wanted to make sure the data was sent and the packet size was fixed)
int xxx = input.readByte();
DataOutputStream os = ply.getOutputStream();
StreamConverter.buffer_write_u16(os, 55321);
os.writeByte((byte) 3);
os.writeByte((byte) xxx);
os.writeByte((byte) xxx);
StreamConverter.buffer_write_u16(os, 8);
StreamConverter.buffer_write_u16(os, 8);
StreamConverter.buffer_write_u16(os, 8);
StreamConverter.buffer_write_u16(os, 8);
StreamConverter.buffer_write_u16(os, 8);
StreamConverter.buffer_write_u16(os, 8);
StreamConverter.buffer_write_u16(os, 8);
os.flush();
My question is obviously: Why are the packets mixing their data? Shouldn't TCP protocol avoid this?
Thanks in advance guys, and let me know if you have any clues or need more info about this.
EDIT: The problem was that threads were sending their data but not in order, that caused the data to arrive mixed. Using a lock made them work as I wanted!
After been struggling with this for about 30 hours by now I've decided to post it here because I don't think I can find an explanation by myself.
Introduction: I've been working on a Java server for a GM:S game, the connection is made through TCP. The Java server connects with GM:S without problem and start sending packets of 19 bytes. The packets are received by the GM:S client but sometimes the packet content is mixed with other packets. I know TCP is streamed so I write all the data received into a buffer and then when that buffer is 19 bytes long (at least) I extract the packet from there.
The problem: From the Java server I'm sending: u16 + byte + byte + byte + u16 + u16 + u16 + u16 + u16 + u16 + u16. So it's: 2+1+1+1+2+2+2+2+2+2+2 = 19bytes. When I read the data I'm receiving the 19 bytes packet should look like this: "25-4033380808080808080", but this packet is sometimes mixed as shown in the following image:
I've highlighted in green the packets that are received fine, but you can see that the "white" packets are mixed.
The data is collected with the following code to avoid any changes from the data received. (This code runs in the Networking event)
buff = ds_map_find_value(async_load,"buffer");
buffer_seek(buff, buffer_seek_start, 0);
repeat(buffer_get_size(buff)) {
file_text_write_real(f,buffer_read(buff, buffer_s8));
}
file_text_writeln(f);
And if it helps, the data is sent from the Java server with the following code. (I know the code is ugly, I just wanted to make sure the data was sent and the packet size was fixed)
int xxx = input.readByte();
DataOutputStream os = ply.getOutputStream();
StreamConverter.buffer_write_u16(os, 55321);
os.writeByte((byte) 3);
os.writeByte((byte) xxx);
os.writeByte((byte) xxx);
StreamConverter.buffer_write_u16(os, 8);
StreamConverter.buffer_write_u16(os, 8);
StreamConverter.buffer_write_u16(os, 8);
StreamConverter.buffer_write_u16(os, 8);
StreamConverter.buffer_write_u16(os, 8);
StreamConverter.buffer_write_u16(os, 8);
StreamConverter.buffer_write_u16(os, 8);
os.flush();
My question is obviously: Why are the packets mixing their data? Shouldn't TCP protocol avoid this?
Thanks in advance guys, and let me know if you have any clues or need more info about this.
EDIT: The problem was that threads were sending their data but not in order, that caused the data to arrive mixed. Using a lock made them work as I wanted!
Last edited: