|
楼主 |
发表于 2004-12-2 13:11:48
|
显示全部楼层
[code:1]
#include <stdio.h>
#include <stdlib.h>
/*
#include <iostream>
#include <iomanip>
*/
#include <unistd.h>
#include <netdb.h>
void output(struct hostent const* p)
{
/*
std::cout<<p->h_name<<std::endl;
std::cout<<(p->(h_addr))<<std::endl;
*/
printf("offical name of host is: %s\n",p->h_name);
int index=1;
char ** pp=p->h_aliases;
while(*pp)
{
printf("alias name %d is :%s\n",index,*pp);
pp++;
index++;
}
printf("address type is :%d , address length is :%d\n",p->h_addrtype,p->h_length);
pp=p->h_addr_list;
index=1;
while(*pp)
{
printf("%s\n",inet_ntoa(*((struct in_addr*)(*pp))));
/*
printf("ip address %d is :%d.%d.%d.%d\n",index,(unsignedchar)(*pp)[0],(unsigned char)(*pp)[1],(unsigned char)(*pp)[2],(unsigned)(*pp)[3]);
*/
pp++;
index++;
}
// printf("%s\n",inet_ntoa(*((struct in_addr*)p->h_addr)));
}
void showHostInfo(char *buffer,size_t size)
{
if(gethostname(buffer,size)==0)
{
printf("hostname is :%s\n",buffer);
struct hostent* phst=gethostbyname(buffer);
output(phst);
}
endhostent();
}
int main(int argc,char* argv[])
{
char buffer[100]="\0";
printf("\n*****************************\n");
showHostInfo(buffer,sizeof(buffer));
printf("*****************************\n\n");
return 0;
}
[/code:1] |
|