提高:: ASIO和插座所有权 [英] boost::asio and socket ownership

查看:140
本文介绍了提高:: ASIO和插座所有权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类(谈判,客户端),两者都有自己的boost ::支持ASIO ::知识产权:: TCP ::插座。有没有一种方法来协商完成后Socket对象传送到客户端。我期待着做这样的事情:

 的boost ::支持ASIO ::知识产权:: TCP ::插座sock1(IO);
 // ...
 提高:: ASIO ::知识产权:: TCP ::插座sock2;
 sock2.assign(sock1);

此操作必须保证当sock1被销毁连接不会被关闭。


解决方案

我的认为的,你可以:


  • 获得sock1的本机句柄与本地()成员函数

  • DUP()(或WSADuplicateSocket())sock1的本地句柄

  • 通过DUP() - 编辑处理与assing()成员函数sock2

不过:<​​/ P>


  • 我不知道,因为我从来没有试过

  • 如果您想要的传输的(而不是共享),从谈判到客户端的插座,使用动态分配的丹的建议可能是简单的,使用的unique_ptr(或auto_ptr的)

I've two classes (Negotiator, Client), both has their own boost::asio::ip::tcp::socket. Is there a way to transfer socket object to Client after negotiation is finished. I'm looking forward to do something like that:

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

This operation must guarantee that the connection won't be closed when sock1 is destroyed.

解决方案

I think that you could:

  • obtain sock1's native handle with the native() member function
  • dup() (or WSADuplicateSocket()) sock1's native handle
  • pass the dup()-ed handle to sock2 with the assing() member function

However:

  • I'm not sure as I never tried that
  • If you want to transfer (instead of sharing) the socket from Negotiator to Client, Dan's suggestion of using dynamic allocation is probably simpler, using unique_ptr (or auto_ptr)

这篇关于提高:: ASIO和插座所有权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆