|
|
发表于 2005-10-2 11:28:27
|
显示全部楼层
sorry,我前面没有说清楚。
gethostbyname也可以使用/etc/hosts来作为域名解析的数据库。只要本机的域名在/etc/hosts中有,就可以用gethostbyname解析出来。
我贴一段代码,你可以试试:
- #include<netinet/in.h>
- #include<arpa/inet.h>
- #include<unistd.h>
- #include<netdb.h>
- #include<stdio.h>
- int main(int argc, char *argv[])
- {
- char *host, **names, **addrs;
- struct hostent *hostinfo;
- if(argc == 1) {
- char myname[256];
- gethostname(myname, 255);
- host = myname;
- }
- else
- host = argv[1];
- hostinfo = gethostbyname(host);
- if(!hostinfo) {
- fprintf(stderr, "cannot get info for host: %s\n", host);
- exit(1);
- }
- printf("results for host %s\n", host);
- printf("Name: %s\n", hostinfo->h_name);
- printf("Aliases: ");
- names = hostinfo->h_aliases;
- while(*names) {
- printf(" %s", *names);
- names++;
- }
- printf("\n");
- if(hostinfo->h_addrtype != AF_INET) {
- fprintf(stderr, "not an ip host!");
- exit(1);
- }
- addrs = hostinfo->h_addr_list;
- while(*addrs) {
- printf(" %s", inet_ntoa(*(struct in_addr *)*addrs));
- addrs++;
- }
- printf("\n");
- exit(0);
- }
复制代码
至于其它可以读取本机域名数据的函数,你可以man gethostent |
|