Increase asio timeout

Possible duplicate:
How to set socket lock timeout in boost asio?

I read some notes before the timeout, but I do not understand.

I need a specific timeout for the connection. The connection code is as follows:

try{ boost::asio::ip::tcp::resolver resolver(m_ioService); boost::asio::ip::tcp::resolver::query query(link.get_host(), link.get_scheme()); boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); boost::asio::ip::tcp::resolver::iterator end; boost::system::error_code error = boost::asio::error::host_not_found; while (error && endpoint_iterator != end) { m_socket.close(); m_socket.connect(*endpoint_iterator++, error); } } 

I also want a read timeout.

I am using boost::asio::read_until(m_socket, response, "\r\n"); to read the title.

Can I set a timeout for SIMPLE?

+6
source share
2 answers

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.

+12
source

Using boost :: asio and synchronous calls like read_until doesn't make it easy to set a timeout.

I would suggest switching to asynchronous calls (e.g. async_read) and combining this with deadline_timer to achieve this.

+3
source

Source: https://habr.com/ru/post/888383/


All Articles