简体   繁体   English

如何从本机套接字创建Boost.Asio套接字?

[英]How to create a Boost.Asio socket from a native socket?

I am merely trying to create a boost ip::tcp::socket from an existing native socket. 我只是想从现有的本机套接字创建一个boost ip::tcp::socket 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. assign函数中 ,第一个参数必须是“protocol_type”,第二个参数必须是“native_type”,但它从不解释它们是什么或给出了它的使用示例。

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" is just the socket handle, in this case the int stored in "socket". “Native type”只是套接字句柄,在这种情况下是存储在“socket”中的int。

"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(). 对于使用流套接字的TCP over标准IP,这将是boost :: asio :: ip :: tcp :: v4()的返回值。 Substitute as appropriate for datagram sockets, IPv6, etc. 适当替换数据报套接字,IPv6等。

So: 所以:

s.assign(boost::asio::ip::tcp::v4(), socket);

Adjusted as appropriate for what you're trying to do. 根据您的尝试进行适当调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM