hostlab.c

来自「find the information about a host with t」· C语言 代码 · 共 86 行

C
86
字号
#include <stdio.h>  /*for printf() and fprintf() */#include <arpa/inet.h> /*for inet_addr() */#include <stdlib.h>  /*for exit() */#include <netdb.h>  /*for gethostbyname() and gethostbyaddr() */int main(int argc, char *argv[] ) {              /*convert IP address in numbers-and-dots notation into unsigned long in network byte order*/   if ( inet_addr(argv[1]) != INADDR_NONE ) {                                char **ptr; // pointer to store the host aliases                int i ;   //variable used in the for loop                struct in_addr addr;                char ip[INET_ADDRSTRLEN];                struct hostent *host;                                                      addr.s_addr = inet_addr(argv[1]);                            /* retrive host entry from DNS and query key is an IP address */                 host = gethostbyaddr( (const char*)&addr, sizeof(ip),AF_INET);//cast to generic address                               if ( host == NULL ) {                       printf("failed\n");                       exit(1);                    }                 printf("h_name\t = %s\n", host->h_name);                 printf("h_addrtype\t=%d\n",host->h_addrtype);                 printf("h_length\t=%d \n",host->h_length);                    ptr = host->h_aliases;                  while(*ptr)  {                        printf("host alias\t =%s\n",*ptr);                        ptr++;                    }                  for ( i=0; host->h_addr_list[i] != NULL; i++ ) {                           //mapping the IP address in network byte order to dotted decimal format                           if ( inet_ntop(AF_INET,host->h_addr_list[i],ip,sizeof(ip) ) != NULL)                                 printf("IP address: %s\n",ip);                           else                                 printf("error\n");                     }                            return 0;                 }   // retrieve host entry from DNS and query key is domain name           else {                             char **ptr;                             int i;                             char ip[INET_ADDRSTRLEN];                             struct hostent *host;                                                        host = gethostbyname(argv[1]);                             if ( host== NULL)                                 {                                     printf("failed\n");                                     exit(1);                                }                              printf("h_name\t = %s\n", host->h_name);                              printf("h_addrtype\t= %d\n",host->h_addrtype);                              printf("h_length\t= %d\n",host->h_length);                                                            ptr = host->h_aliases;                                                           while(*ptr) {                                        printf("host alias\t=%s\n", *ptr);                                          ptr++;                                     }  // end while                                                   for ( i=0; host->h_addr_list[i]!= NULL; i++ )  {                                    if(inet_ntop(AF_INET, host->h_addr_list[i],ip,sizeof(ip) ) != NULL )                                             printf("IP  address: %s\n",ip);                                    else                                        printf("error");                             }  // end for                               return 0;             } // end else                   return 0;       }  // end main

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?