linux 通過Ip擷取主機名稱等資訊gethostbyaddr()等。

來源:互聯網
上載者:User
#include <stdlib.h>#include <stdio.h>#include <netdb.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>int main(int argc, char **argv){char *ptr,**pptr;struct hostent *hptr;char str[32];char ipaddr[16];struct in_addr *hipaddr = (struct in_addr *)malloc(sizeof(struct in_addr));ptr = argv[1];printf("0:%s\n",ptr);if(!inet_aton(ptr,hipaddr)){printf("error1\n");return 1;}if( (hptr = gethostbyaddr(hipaddr,4,AF_INET) ) == NULL){h_errno;printf("err2 %s\n",ptr);switch(h_errno){case HOST_NOT_FOUND:printf("111\n");break;//case NO_ADDRESS://case NO_DATA:printf("112\n");break;case NO_RECOVERY:printf("113\n");break;case TRY_AGAIN:printf("115\n");break;}return 1;}printf("hostname:%s\n",hptr->h_name);for(pptr = hptr->h_aliases; *pptr != NULL; pptr++ )printf("%s\n",*pptr);switch( hptr->h_addrtype){case AF_INET:case AF_INET6: pptr = hptr->h_addr_list;for(;*pptr!=NULL;pptr++)printf("address:%s\n",inet_ntop(hptr->h_addrtype,*pptr,str,sizeof(str)));break;default:printf("default \n");break;}return 0;}/* * //http://www.rosoo.net/a/201105/11535.html *傳回值的gethostbyname()和gethostbyaddr()函數功能返回的 HOSTENT的結構或NULL指標,如果出現錯誤。錯誤時,h_errno的變數儲存的錯誤號碼。錯誤的可變h_errno的可以具有以下值:HOST_NOT_FOUND指定的主機是未知的。NO_ADDRESS或NO_DATA請求的名稱是有效,但沒有一個IP地址。NO_RECOVERY不可恢複的名稱伺服器發生錯誤。TRY_AGAIN一個臨時錯誤發生在權威網域名稱伺服器。請稍後再試。 * */

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.