how to turn C++/CLI .Net socket into boost::asio socket?

╄→гoц情女王★ 提交于 2019-12-07 13:09:02

问题


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

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

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

回答1:


Have you tried?

b.assign(a.Handle.ToInt32());

Also note they you will need to use WSADuplicateSocket as you may get that both b and a will close the socket and point you don't expect.

So you need something like:

 SOCKET native = WSADuplicateSocket(a.Handle.ToInt32(),...);
 b.assign(native);

Full Answer (Tested)

SOCKET native = a->Handle.ToInt32();
// Now native is a real socket
b.assign(boost::asio::ip::tcp::v4(), native);

Now it is good idea to duplicate the socket using WSADuplicateSocket:




回答2:


You can use assign to assign a native socket to a Boost asio socket. See the accepted answer to How to create a Boost.Asio socket from a native socket.



来源:https://stackoverflow.com/questions/7361736/how-to-turn-c-cli-net-socket-into-boostasio-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!