【问题标题】:IOS boost asio connect from ipv6 networkIOS从ipv6网络提升asio连接
【发布时间】:2016-09-05 07:15:19
【问题描述】:

我正在尝试使用 ios 中的 boost asio 库连接 dvr。该应用程序在具有 ipv4 网络的模拟器中运行良好。但是当我在 Appstore 上提交申请时,苹果拒绝了该申请,因为它不适用于 ipv6 网络。我可以在苹果网站上看到应用程序应该支持 ipv6 网络。 https://developer.apple.com/news/?id=05042016a

所以我认为问题出在我尝试使用 boost 库连接到 DVR 的部分,其中 DVR 的 IP 地址是从 DB(硬编码)中提取的,下面是代码的相关部分。

        boost::asio::io_service io_service_;
        tcp::resolver::iterator endpoint_iter_;
        tcp::resolver resolver_; //to healp resolving hostname and ip

        stringstream strstream;//create a stringstream
        strstream << port;//add number to the stream

        endpoint_iter_ = resolver_.resolve(tcp::resolver::query(ip.c_str(),strstream.str()));
        start_connect(endpoint_iter_);

        // Start the deadline actor. You will note that we're not setting any
        // particular deadline here. Instead, the connect and input actors will
        // update the deadline prior to each asynchronous operation.
        deadline_timer_.async_wait(boost::bind(&dvr_obj::check_deadline, this));
        //starting thread for dvr connection
        io_service_.reset();
        thread_ =  new boost::thread(boost::bind(&boost::asio::io_service::run, &io_service_));

start_connect方法

void start_connect(tcp::resolver::iterator endpoint_iter)
{
    try
    {
        if (endpoint_iter != tcp::resolver::iterator())
        {

            drill_debug_info("trying to connect %s \n",name.c_str());

            // Set a deadline for the connect operation.
            deadline_timer_.expires_from_now(boost::posix_time::seconds(10));

            // Start the asynchronous connect operation.
            socket_.async_connect(endpoint_iter->endpoint(),
                                  boost::bind(&dvr_obj::handle_connect,
                                              this, _1, endpoint_iter));
        }
        else
        {
            // There are no more endpoints to try. Shut down the client.

            connectivity = false;
        }
    } catch (int e) {

        connectivity = false;
    }
}

所以我很困惑如何更改上述代码以在 IPV6 网络上工作。在 Internet 上找不到任何解决方案。

【问题讨论】:

  • resolver_.resolve() 将迭代器返回到条目列表。您的代码只使用第一个条目。如果您遍历列表,您可能会找到一个 IPv6 条目...
  • 感谢回复,能否给我一些参考代码。

标签: ios objective-c boost boost-asio


【解决方案1】:

您可以使用以下代码遍历端点以查找 IPv6 端点:

endpoint_iter_ = resolver_.resolve(tcp::resolver::query(ip.c_str(),strstream.str()));

while (endpoint_iter_ != tcp::resolver::iterator())
{
  if (endpoint_iter_->endpoint().protocol() == tcp::v6())
    break;
  ++endpoint_iter_;
}

if (endpoint_iter_ != tcp::resolver::iterator())
{
   start_connect(endpoint_iter_);
   ...
}
else
   std::cerr << "IPv6 host not found" << std::endl;

【讨论】:

  • 谢谢,我会试试你的答案,让你知道结果。
  • if (endpoint_iter_-&gt;protocol() == tcp::v6()) 给我错误 no member named..
  • 我将上面替换为boost::asio::ip::address addr = endpoint_iter_-&gt;endpoint().address(); if(addr.is_v6()) break; ++endpoint_iter_;
  • 应用程序也应该在 ipv6/ipv4 网络上运行。
  • 我很高兴你设法修正了我给你的例子。但是,如果有可用的 IPv6 端点,它应该会找到它。你试过用wireshark查看网络流量吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-22
  • 1970-01-01
相关资源
最近更新 更多