I have a program running on a server communicating with another program running on the client. They both send data and files back and forth.
I notice that whenever there is a socHandler.read() (function to read data coming in on socket), it gets stuck waiting for data to arrive.
This is what the function looks like.
int CSocHandler::read(char * inBuffer, INT64 iBytesToRead){
bool blReadMore=false;
int retVal = 0;
int iOutputPointer = 0;
do{
blReadMore = false;
if (iStartOfBuffer == iEndOfBuffer) { //Buffer empty
if (blClosed ) {
return -1;
}
iStartOfBuffer = 0;
iEndOfBuffer = 0;
size_t bufferRemaining = BUFFER_SIZE-iEndOfBuffer;
int bytesRecvd = recv( sock, &buffer[iEndOfBuffer], (int)iBytesToRead<bufferRemaining?iBytesToRead:bufferRemaining), 0 );
if ( bytesRecvd <= 0) {
close();
Yield();
return retVal;
}
}
} while(blReadMore);
return retVal;
}
The variable sock is a type SOCKET and is a global variable defined elsewhere. How can I set the options or make this recv() call non-blocking only for this function call and no affect any other function?
I believe this is where it gets stuck waiting for data, and I want to make it timeout after X seconds.
Call
select()to query if the socket has any pending data before then callingrecv()to read it.