0
我想用gethostbyname()创建一个in_addr数组。寻找在谷歌之后,我发现这个短码(在http://www.logix.cz/michal/devel/various/gethostbyname.c.xp):in_addr数组
/*
* gethostbyname.c - Example of using gethostbyname(3)
* Martin Vidner <[email protected]>
*/
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
struct hostent *he;
struct in_addr a;
int
main (int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "usage: %s hostname\n", argv[0]);
return 1;
}
he = gethostbyname (argv[1]);
if (he)
{
printf("name: %s\n", he->h_name);
while (*he->h_aliases)
printf("alias: %s\n", *he->h_aliases++);
while (*he->h_addr_list)
{
bcopy(*he->h_addr_list++, (char *) &a, sizeof(a));
printf("address: %s\n", inet_ntoa(a));
}
}
else
herror(argv[0]);
return 0;
}
我测试此代码与此:
$ ./a.out google.com
name: google.com
address: 74.125.45.100
address: 74.125.53.100
address: 74.125.67.100
看到这样的结果,我很满意,因为我想的列表来自域的IP地址。
但我只是有一个问题:我不知道如何保存IP地址列表中一个阵列中(与同尺寸比地址数)......你有偶然的例子吗?
谢谢