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?
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:and don't forget
#include <boost/asio.hpp>
test run:
PS: For reference, I called
""