C/C++ URL转IP
#include <spdlog/spdlog.h>
#include <cstring>
#include <netdb.h>
#include <cassert>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc,char* argv[]){
assert(argc >= 2);
hostent * ent = gethostbyname(argv[1]);
assert(ent != nullptr);
sockaddr_in addr;
memcpy(&addr.sin_addr,ent->h_addr,ent->h_length);
auto ip_addr = inet_ntoa(addr.sin_addr);
spdlog::info("ip address of host[{0}] is {1}",argv[1],ip_addr);
return 0;
}
g++ -o url2ip -std=c++11 -lspdlog url2ip.cc
./url2ip www.baidu.com