How to create a Boost.Asio socket from a native socket?(如何从本机套接字创建 Boost.Asio 套接字?)
问题描述
我只是想从现有的本机套接字创建一个 boost ip::tcp::socket
.在 assign 函数中,第一个参数必须是protocol_type",第二个参数必须是native_type",但它从不解释它们是什么或给出其使用示例.
I am merely trying to create a boost ip::tcp::socket
from an existing native socket. In the assign function, the first parameter must be a "protocol_type" and the second must be a "native_type", but it never explains what these are or gives an example of its use.
我猜第二个应该是套接字描述符,但我非常感谢澄清.
I'm guessing the second should be the socket descriptor, but I'd really appreciate clarification.
void SendData (int socket, std::string message)
{
boost::asio::io_service ioserv;
boost::asio::ip::tcp::socket s(ioserv);
s.assign(/* what goes here? */, /* ..and here? */);
s.send(boost::asio::buffer(message));
}
推荐答案
Native type"只是socket句柄,这里是socket"中存储的int.
"Native type" is just the socket handle, in this case the int stored in "socket".
协议类型"是协议.对于使用流套接字的标准 IP 上的 TCP,这将是 boost::asio::ip::tcp::v4() 的返回值.酌情替代数据报套接字、IPv6 等.
"Protocol type" is the the protocol. For a TCP over standard IP using stream socket, this would be the return value from boost::asio::ip::tcp::v4(). Substitute as appropriate for datagram sockets, IPv6, etc.
所以:
s.assign(boost::asio::ip::tcp::v4(), socket);
根据您的尝试进行适当调整.
Adjusted as appropriate for what you're trying to do.
这篇关于如何从本机套接字创建 Boost.Asio 套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从本机套接字创建 Boost.Asio 套接字?


基础教程推荐
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01