问题
I have an interesting problem,
- I send bytes of data to my server, from my client, and the recv() function always returns zero.
- I have verified that the function is indeed getting the data and placing it into the char array correctly. It just returns zero regardless of the number of bytes received.
- I have verified that the client is still properly connected after recv() returns a zero, and can still send and receive data to and from the server.
This creates a problem for me. I need to total up the chars coming in to determine when a message is completed. Can someone explain what could cause the recv() command to behave in this manner?
// clean the buffer of any previous received data
// by resetting all bytes to zero
ReadBufferClear();
// try to read some data, using the read buffer
if(this->Return_Value = recv(this->Socket_Filedescriptor,
this->ReadBuffer_Data,
this->ReadBuffer_Size,
0) == -1)
{
// returned -1, notify the server it needs
// to remove this connections FD, and
// close this connection class entry;
return -1;
}
// print return value
fprintf(stdout,"%i\n",this->Return_Value);
// print out the bytes
for(int curPos =0; curPos<this->ReadBuffer_Size; curPos++)
fprintf(stdout,"%i\n",this->ReadBuffer_Data[curPos]);
output looks like this:
0 0 1 -1 -1 -1 -2 -1 -1 0 0
回答1:
You have a precedence problem. The line should be of the form
if ((this->ReturnValue = recv(..).)) == -1)
At present you're comparing the result of recv() with -1 and storing the Boolean result of that comparison into ReturnValue.
So recv() isn't returning zero at all.
来源:https://stackoverflow.com/questions/23866637/recv-returning-zero-incorrectly