Fist of all, I believe that you should ALWAYS use asynchronous analysis methods, as they are better, and your design will be useful only for the approach using the reactor circuit. In the bad case that you are in a hurry and you are a prototype, synchronization methods can be useful. In this case, I agree with you that without the support of a timeout, they cannot be used in the real world.
What I did was very simple:
void HttpClientImpl::configureSocketTimeouts(boost::asio::ip::tcp::socket& socket) { #if defined OS_WINDOWS int32_t timeout = 15000; setsockopt(socket.native(), SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout)); setsockopt(socket.native(), SOL_SOCKET, SO_SNDTIMEO, (const char*)&timeout, sizeof(timeout)); #else struct timeval tv; tv.tv_sec = 15; tv.tv_usec = 0; setsockopt(socket.native(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); setsockopt(socket.native(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); #endif }
This code runs on windows as well as Linux and MAC OS according to the OS_WINDOWS macro.
source share