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 套接字?
基础教程推荐
- 提升 ASIO 流缓冲 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- c++ STL设置差异 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
