1. 程式人生 > >通過gethostbyaddr來獲取計算機的名稱

通過gethostbyaddr來獲取計算機的名稱


         unsigned 
long ddd = inet_addr(*argv);
         
if(ddd != INADDR_NONE)
         

           host
=gethostbyaddr((char*)&ddd,4,AF_INET); 
         }
  
         
else  
         

              host
=gethostbyname(*argv);
         }
 
        
if(host==NULL) 
        

              fprintf(stderr,
"No address information of %s ",*argv);           
               
return1;
        }
 
        printf(
"Official host name %s ",host->h_name); 
        printf(
"Name aliases:"); 
        
for(alias=host->h_aliases;*alias!=NULL;alias++
          printf(
"%s ,",*alias); 
        printf(
" Ip address:"); 
        
for(alias=host->h_addr_list;*alias!=NULL;alias++
          printf(
"%s ,",inet_ntoa(*(struct in_addr *)(*alias))); 
    }