I was reading this winsock example.
I am trying to conceptualize how you would create a C++ client program that has a persistent TCP/IP connection to a C# .NET server.
The problem I see is that in order for the C++ client to leave the receive loop, the server must close its socket connection to the client.
In my case the server will send to the client every couple seconds. I need to be able to receive one packet from the server and restart the main program loop so the client can perform the rest of its functionality.
If this receive code is in the C++ client’s main loop, the client will never stop receiving if the server never closes the connection to the client:
// Receive until the peer closes the connection
do {
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
printf("Bytes received: %d\n", iResult);
else if ( iResult == 0 )
printf("Connection closed\n");
else
printf("recv failed with error: %d\n", WSAGetLastError());
} while( iResult > 0 );
The example program that you have chosen to work from is designed to send a single request and receive a single response. It uses the state of the connection to indicate the end of the request, and the end of the response.
You might want to work from a different example program. Search for “winsock chat example” on google.
On the other hand, to modify this program as you have asked, you could replace the do-while loop with this: