Boost :: asio and socket

I have two classes (Negotiator, Client), both have their own boost :: asio :: ip :: tcp :: socket. Is there any way to pass the socket object to the client after negotiation is complete. I hope to do something like this:

boost::asio::ip::tcp::socket sock1(io); //... boost::asio::ip::tcp::socket sock2; sock2.assign(sock1); 

This operation should ensure that the connection is not closed when sock1 is destroyed.

+3
source share
3 answers

I think you could:

  • get native sock1 descriptor with native () member function
  • dup () (or WSADuplicateSocket ()) native handle sock1
  • pass handle dup () - ed to sock2 using the member function assing ()

But:

  • I am not sure as I have never tried this.
  • If you want to transfer (instead of sharing) a socket from Negotiator to the client, Dan's suggestion to use dynamic allocation is probably easier using unique_ptr (or auto_ptr)
+2
source

Create a socket on the heap (new) and pass the pointer from the negotiator to the client.

+2
source

Starting with the current version of Boost, you will now receive a handle with

 boost::asio::ip::tcp::socket::my_socket; auto my_handle = my_socket.native_handle(); 

instead of the old native() member function.

0
source

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


All Articles