如何创建从本地插座一个插座Boost.Asio的? [英] How to create a Boost.Asio socket from a native socket?

查看:146
本文介绍了如何创建从本地插座一个插座Boost.Asio的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想创建从现有的本地插座提振 IP :: TCP ::插座。在<一个href=\"http://www.boost.org/doc/libs/1%5F37%5F0/doc/html/boost%5Fasio/reference/basic%5Fstream%5Fsocket/assign.html\">assign功能,第一个参数必须是一个协议类型,第二个必须是一个native_type,但它没有解释这是什么意思,或提供其使用的一个例子。

我猜第二个应该是socket描述符,但我真的AP preciate澄清。

 无效送出数据(INT插座的std ::字符串消息)
{
    提高:: ASIO :: io_service对象ioserv;
    提高:: ASIO ::知识产权:: TCP ::插座S(ioserv);
    s.assign(/ *善有善报这里* / / * ..和这里* /?);
    s.send(升压:: ASIO ::缓​​冲区(消息));
}


解决方案

本土型只是套接字句柄,在这种情况下的int保存在插口。

协议类型是的协议。对于使用流式套接字通过标准IP通过TCP,这将是自boost :: ASIO ::知识产权:: TCP :: V4()的返回值。替代为适合数据报套接字,IPv6的,等等。

所以:

  s.assign(升压:: ASIO ::知识产权:: TCP :: V4(),插座);

调整为适合你想要做的事。

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" is just the socket handle, in this case the int stored in "socket".

"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.

So:

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

Adjusted as appropriate for what you're trying to do.

这篇关于如何创建从本地插座一个插座Boost.Asio的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆