More bytes sent than what I should receive via socket
开发者_StackOverflowin my application I found that I receive 2 bytes more than what I expect. I don't know what these 2 bytes means because in the client I'm logging the bytes sent and I receive all on the server, but after the data I still receive these 2 bytes that crash my program. Why? Any idea?
EDIT 1
on the client
int sentBytes = send(socket, data, size, 0);
and on the server
int receivedBytes = SOCKET_ERROR;
char *header = new char[8];
receivedBytes = recv(socket, header, 8, 0);
if (receivedBytes == 0 || receivedBytes == WSAECONNRESET)
{
...
}
else
{
size_t data_size = 0;
std::istringstream is(std::string(header, 8));
if (!(is >> std::hex >> data_size))
{
char *data = new char[data_size + 1];
data_size = recv(socket, data, data_size, 0);
}
}
I'm using the win32 message loop with WSAAsyncSelect
EDIT 2
I'm using the first 8 bytes to send an hex value that contains the length of the message that I should receive on the server
- You don't check that you got exactly 8 bytes on the first read. It might be less.
- I hope this is TCP, since with UDP you'd be reading the next packet with the second
recv()
.
精彩评论