在C++中,如何使用线程在Sockets中不阻止接收功能?使用C++在Windows中进行线程化操作
// Receive until the peer closes the connection
do {
iResult = recv(lhSocket, recvbuf, recvbuflen, 0);
if (iResult > 0)
printf("Bytes received: %d\n", iResult);
else if (iResult == 0)
printf("Connection closed\n");
else
printf("recv failed: %d\n", WSAGetLastError());
} while(iResult > 0);
closesocket(lhSocket);
WSACleanup();
我对此并不十分确定,但我认为使用Boost.Asio可能会更容易,因为它是异步的,完全跨平台的,并且不需要使用线程。 http://www.boost.org/doc/libs/1_44_0/doc/html/boost_asio.html – 2010-08-18 11:24:09