如何使用 boost::asio 将 URL 转换为 IP 地址?

发布于 2024-10-28 22:23:36 字数 1159 浏览 3 评论 0原文

所以我需要某种方法将给定的 Protocol://URLorIP:Port 字符串转换为字符串 ip int port 如何使用 boost ASIO 来做这样的事情和增强正则表达式?或者是否有可能 - 使用 C++ Net Lib(增强候选)获取 IP - 注意 - 我们这样做不需要长连接——只需要IP。

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

#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?

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;
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信愁 2024-11-04 22:23:36

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

使用相当简单,在程序的 return 0; 之前添加它:

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

测试运行:

~ $ 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:作为参考,我调用了

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

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';

and don't forget #include <boost/asio.hpp>

test run:

~ $ 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: For reference, I called

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文