如何从本机套接字创建 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.
相关文章