如何使用boost :: asio将URL转换为IP地址? [英] How to turn URL into IP address using boost::asio?

查看:250
本文介绍了如何使用boost :: asio将URL转换为IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要某种方式将给定的Protocol://URLorIP:Port字符串转换为字符串ip int port如何使用boost ASIO和Boost Regex来做这些事情?还是有可能-使用 C ++ Net Lib (增强候选)来获取IP-注意-我们做到了不需要长连接-仅IP.

So I need some way of turning given Protocol://URLorIP:Port string into string ip int port How to do such thing with boost ASIO and Boost Regex? Or is it possible - to get IP using C++ Net Lib (boost candidate) - notice - we do not need long connection - only IP.

所以我目前使用这样的代码进行解析

So I currently use such code for parsing

#include <boost/regex.hpp>
#include <vector>
#include <string>

int main(int argc, char** argv)
{
    if (argc < 2) return 0;

    std::vector<std::string> values;
    boost::regex expression(
        //   proto                 host               port
        "^(\?:([^:/\?#]+)://)\?(\\w+[^/\?#:]*)(\?::(\\d+))\?"
        //   path                  file       parameters
        "(/\?(\?:[^\?#/]*/)*)\?([^\?#]*)\?(\\\?(.*))\?"
    );
    std::string src(argv[1]);
    if (boost::regex_split(std::back_inserter(values), src, expression))
    {
        const char* names[] = {"Protocol", "Host", "Port", "Path", "File", 
                "Parameters", NULL};
        for (int i = 0; names[i]; i++)
            printf("%s: %s\n", names[i], values[i].c_str());
    }
    return 0;
}

我应该在我的小程序中添加些什么以将URL解析为IP?

What shall I add to my small program to parse URL into IP?

推荐答案

请记住,一个主机名可能有多个IP地址,boost为您提供了一个遍历它们的迭代器.

Remember that there may be multiple IP addresses for any one hostname, boost gives you an iterator that will go through them.

用法非常简单,请将其添加到程序的return 0;之前:

The use is fairly straightforward, add this before return 0; of your program:

std::cout << "IP addresses: \n";
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query(values[1], "");
for(boost::asio::ip::tcp::resolver::iterator i = resolver.resolve(query);
                            i != boost::asio::ip::tcp::resolver::iterator();
                            ++i)
{
    boost::asio::ip::tcp::endpoint end = *i;
    std::cout << end.address() << ' ';
}
std::cout << '\n';

别忘了#include <boost/asio.hpp>

试运行:

~ $ g++ -g -Wall -Wextra -pedantic -Wconversion -ansi -o test test.cc -lboost_regex -lboost_system -lboost_thread
~ $ ./test http://www.google.com:7777
Protocol: http
Host: www.google.com
Port: 7777
Path:
File:
Parameters:
IP addresses:
74.125.226.179 74.125.226.176 74.125.226.178 74.125.226.177 74.125.226.180

PS:我打电话给

  • TCP resolver's constructor
  • query's host/service constructor with a don't-care service value of ""
  • the exception-throwing form of resolve()
  • dereferenced the iterator to get a resolver entry
  • used resolver_entry's type conversion to endpoint
  • used the TCP endpoint's address() accessor
  • used operator<< to show the address: you could use to_string() instead, if needed.

这篇关于如何使用boost :: asio将URL转换为IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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