boost asio: Probelm with closing a blocking ip::udp::socket receive
Hi, I'm using boost 1.35 on Linux. I have a thread which initiates a ip::udp::socket and has a blocking receive. When I try to terminate this thread from a different thread, in order to close the udp::socket I use the following: // m_socket is of type ip::udp::socket boost::system::error_code ec; m_socket::shutdown(boost::asio::ip::udp::socket::shutdown_receive, ec); std::cout << "error code = " << ec << std::endl; m_socket.close(); After shutdown I ALWAYS get system:107 error code - which means "Transport endpoint is not connected". Does anybody have any idea why I'm receiving such an error? Needless to say, the receive is working great - and I get the packets from the port I'm listening to. Thanks!
After shutdown I ALWAYS get system:107 error code - which means "Transport endpoint is not connected".
Does anybody have any idea why I'm receiving such an error? Needless to say, the receive is working great - and I get the packets from the port I'm listening to.
I don't know why you get this error, but socket object is not threadsafe, so you shouldn't access it from different threads without synchronization.
OK, thanks, but how am I supposed to close a thread which has a blocking receive in it? Igor R wrote:
After shutdown I ALWAYS get system:107 error code - which means "Transport endpoint is not connected".
Does anybody have any idea why I'm receiving such an error? Needless to say, the receive is working great - and I get the packets from the port I'm listening to.
I don't know why you get this error, but socket object is not threadsafe, so you shouldn't access it from different threads without synchronization. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
OK, thanks, but how am I supposed to close a thread which has a blocking receive in it?
Probably, in the current impelmentation of ASIO you can use the technique described in the following discussion: http://www.nabble.com/-asio--sync-receive_from-with-timeout-tt22326399.html#... But in general, how would you "close" blocking recv() (for example, in Winsock)? You can terminate its thread, but then you can't be sure that the socket is in a valid state. Usually, if you're going to make non-trivial i/o with asio (involving timeouts etc.), then asynchronous i/o is the way to go.
participants (2)
-
Igor R
-
Lennyk