Convert hostname to IPAddr
How do I convert from a hostname (e.g. 'myhost'开发者_如何学编程) to a type of IPAddr
using Windows API. IPAddr
is an unsigned long.
Try this (edited):
hostent * record = gethostbyname(argv[1]);
if(record == NULL)
{
printf("%s is unavailable\n", argv[1]);
exit(1);
}
in_addr * address = (in_addr * )record->h_addr;
string ip_address = inet_ntoa(* address);
IPAddr dst_ip = ::inet_addr( ip_address.c_str() );
精彩评论