Click to See Complete Forum and Search --> : Received 34 bytes but packet is 12 bytes?


Clash
June 8th, 2007, 08:55 AM
Err hi... it's me again. I did understand that sometimes packets will come together, but what I'm not understanding is that it's saying I received 34 bytes, yet when I cout the packet it's only 12 bytes...

I took a screenshot:
http://xs216.xs.to/xs216/07235/wadahell2.png
Top part is the server, bottom is client
This is the format: (Packet) Received X Byes
You can notice that the 3 sent packets became 1 big one. The client says it received the big one, but when I cout it, it's only the first packet

Client code for receiving packets:
int kBufferSize = 1024;
char acReadBuffer[kBufferSize];
int nReadBytes;
do {
nReadBytes = recv(s, acReadBuffer, kBufferSize, 0);
if (nReadBytes > 0) {
hasNewData = true;
cout << "(" << acReadBuffer << ") Received " << nReadBytes << " bytes \n";
levelCommands->interpretatePackets(acReadBuffer);
}
else if (nReadBytes == SOCKET_ERROR) {
return false;
}
} while (nReadBytes != 0);
Do you guys need any other part of the code?

Thanks!

Richard.J
June 8th, 2007, 11:41 AM
most probably, the data you received contains binary nulls and cout stops its output at the first occurence.

Clash
June 8th, 2007, 12:00 PM
Oh!! I think know what is it!
It's because when a string is actually 11 chars long, I send 12, meaning it's send the \0, when other packets come together it'd look like this: 5 381 3189/05 1 3

When it sees the \0 it stops, right?

So, what's that command so I can insert a \0 at the end instead of sending it via the packet?

MikeAThon
June 8th, 2007, 02:58 PM
Yes, as Richard pointed out, the terminating NULL is "confusing" cout, so that it stops outputting after the first NULL that it encounters.

You need to decide whether your sender will or will not send the NULL character. According to the protocol that you described in other posts, you are prefixing each message with the count of bytes in the message, so you really don't need to send the NULL. Moreover, if you do, then your count should properly reflect that, i.e., if you send "0091 34 67 9", whereas the data sent actually includes a terminating NULL, then your message is 10 bytes long, not 9.

Mike