我需要一种将给定的Protocol://URLorIP:Port
字符串转换为ip
和port
的方法。如何在Boost ASIO和Boost Regex中实现这样的功能?或者是否可以使用C++ Net Lib(Boost候选库)获取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?
getaddrinfo
来完成)。由于我不知道 boost ASIO,所以我不会将其作为答案,而只是提供建议。 - ereOn