I’m using the recv function in a loop to receive network data, but let’s say I want to stop receiving data mid-loop. I could just break the loop, but this doesn’t seem like a very clean way to stop receiving data.
So is there any way to cleanly stop receiving data, or is just breaking the loop ok? It’s HTTP GET/POST requests.
Here’s simplifed I’m using:
do {
nDataLen = recv(mySocket, data, BUFFSIZE, 0);
if (nDataLen > 0)
{
/* Process Data */
// I'd like to break out of the loop
// if something is found when processing the data
// But, I want to do this cleanly.
}
} while (nDataLen != 0);
Breaking out of the recv loop won’t close the connection. All that happens is that you stop calling recv and therefore stop reading data from the socket. Data from the peer will still be arriving on your socket. If you want to cleanly shutdown then call
closeon the socket file descriptor.