Windows system >> Windowsの知識 >  >> Linuxシステムチュートリアル >> Linuxチュートリアル >> LinuxのDNSとのgethostbynameのgetaddrinfo

LinuxのDNSとのgethostbynameのgetaddrinfo

  

関数プロトタイプの#include< netdb.h>構造体たhostent *のgethostbyname(CONST文字*名);アクション:プロトタイプは、以下の構造をれるhostentドメイン名を解決するために使用することができる:構造体たhostent {するchar * h_name;チャー** h_aliases; int型h_addrtype; int型h_length;チャー** h_addr_list;}例#include< stdio.hの>の#include< STDLIB.H>の#include< SYS /socket.hに>。 #include< netinetの/in.h>の#include< netdb.h> int型のmain(int型ARGC、チャー** ARGV){IF(ARGC = 2!){関数fprintf(stderrに、"使用:%sのホスト名\\ N",のargv [1]);出口(1);}構造体たhostent *答え; iがint型; [16] ipstrチャー;答え=のgethostbyname(ARGV [1]); IF(回答== NULL){herror("のgethostbyname"); //のgethostbyname自身のエラー処理機能の出口(1);(answer-> h_addr_list}(i = 0)[i]は= NULL;! iは++){inet_ntop(AF_INET、(解答 - > h_addr_list)[i]は、ipstr、16);のprintf("%S \\ N" ipstr);のprintf(" TTMA公認名:%S \\ N" answer-> h_name);}出口( 0);結果の}コンパイラの実装:ルート@ Ubuntuの:名www.hpu.edu.cn202.102.253.254officail /メディア/2-G /コード教師/20100427 /inet_v4 /ストリーム#の./myhost:第二に、関数プロトタイプint型のgetaddrinfo(www.hpu.edu.cn constのchar *構造体のaddrinfoノード、constのchar型*サービス、constの構造体のaddrinfo *ヒント、** RES);この機能リストデータが格納されています。チャー・ノード一般CONSTのchar *サービス//ドメイン名サービス、構造体RES例示的ADDRINFOのソケットアドレスによって返されるかもしれないNULLconst構造体のaddrinfo *ヒント//ポイント構造**解像度//返される結果:の#include<。 stdio.hの>の#include< STDLIB.H>の#include< SYS /socket.hに>の#include< netinetの/in.h>の#include< netdb.h>の#include< string.hの> int型メイン(INT ARGC、チャー** ARGV){IF(ARGC = 2!){関数fprintf(stderrに、"使用:%sのホスト名\\ n",のargv [1]);出口(1);}構造体のaddrinfo *答え、ヒント、* curr; char ipstr [16]; bzero(& hint、sizeof(hint)); hint.ai_family = AF_INET; hint.ai_socktype = SOCK_STREAM; int ret = getaddrinfo(argv [1]、NULL、& hint) 、&​​#038;回答); IF(RET = 0){;のgetaddrinfo関数fprintf(stderrに、":&#038; S \\ n", gai_strerror(RET);出口(1);!)}; CURR =(CURR =答えるため! NULL; CURR = curr->のai_next){inet_ntop(AF_INET、&#038;(((構造体のsockaddr_in *)(curr-> ai_addr)) - > sin_addr)、ipstr、16);のprintf("%S \\ N" ipstr);} freeaddrinfo(回答);出口(0);}
zh-CN"],null,[0.95981765],zh-CN"]]]

Copyright © Windowsの知識 All Rights Reserved