How should I exit the recv loop?

I use the recv function in the loop to get network data, but let me say that I want to stop receiving data in the middle of the loop. I could just break the loop, but that doesn't seem like a very clean way to stop receiving data.

So, is there a way to clear the data reception or just close the loop? These are HTTP GET / POST requests.

Here's a simplified use:

 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); 
+4
source share
3 answers

Shutting down the recv loop will not close the connection. All that happens is that you stop calling recv and therefore stop reading data from the socket. Data from the partner will still arrive on your socket. If you want to perform a clean shutdown, call close on the socket file descriptor.

+3
source

Exiting a loop is a way to stop the recv call. Another is to include the logical should_stop and check it in the loop condition. However, you should receive everything that remains in the slot and properly close it.

0
source

If you do not need a socket, close the socket and exit the loop.

0
source

All Articles