如何将升压:: ASIO插座为C ++ / CLI净插座? [英] how to turn boost::asio socket into C++/CLI .Net socket?

查看:124
本文介绍了如何将升压:: ASIO插座为C ++ / CLI净插座?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要的很简单 - code创建从升压ASIO插座新的C ++ / CLI的.Net插座样品。如何创建这样的事?

What I want is simple - code sample of creating new C++/CLI .Net socket from boost asio socket. How to create such thing?

下面是什么,我想这样做伪code:

Here is pseudo code of what I would like to do:

.net::socket a;
boost::asio::socket b;
a.assign(b.nativeWin32Socket());

BTW:这里是如何turn C ++ / CLI的.Net插座到升压:: ASIO插座。

推荐答案

您不能'分离'一个Boost.Asio的插座。您可以使用函数native_handle()成员函数来获得 SOCKET 处理ASIO: :插座的对象,但你必须确保 ASIO ::插座对象不破坏,直到你与<$ C $完成C> SOCKET 。它继续保持本机的所有权 SOCKET 键,将其关闭时,它的析构函数被调用。

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.

像安德烈建议,你可以重复套接字句柄。不过,我不会考虑重复这种插座安全的,因为Boost.Asio的会自动将本机 SOCKET 处理与I / O完成端口。如果.NET插座包装或其他一些code尝试复制的插座不同的I / O完成端口相关联,就会发生错误。我知道,.NET 2.0 Socket类呢,其实,关联 SOCKET 处理与异步操作的I / O完成端口。这可能是在新的版本却改变了。

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.

这篇关于如何将升压:: ASIO插座为C ++ / CLI净插座?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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