You can't 'detach' a Boost.ASIO socket. You can use the native_handle()
member function to get a SOCKET
handle from the asio::socket
object, but you must ensure that the asio::socket
object isn't destructed until you're done with the SOCKET
. It continues to hold ownership of the native SOCKET
and will close it when it's destructor is called.
Like André suggested, you could duplicate the socket handle. However, I wouldn't consider duplicating this socket safe, because Boost.ASIO automatically associates the native SOCKET
handle with an I/O completion port. If the .NET Socket wrapper or some other code attempts to associate the duplicated socket with a different I/O completion port, an error will occur. I know that the .NET 2.0 Socket class does, in fact, associate the SOCKET
handle with an I/O completion port for asynchronous operations. This may have changed in more recent versions, however.