开发者

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() );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜