基本介紹
詞語釋義,詳細資訊,地址,Examples,
詞語釋義
hostent的定義如下:
struct hostent{char * h_name;char ** h_aliases;short h_addrtype;short h_length;char ** h_addr_list;#define h_addr h_addr_list[0];};
詳細資訊
地址
struct hostent
h_name – 地址的正式名稱。
h_aliases – 空位元組-地址的預備名稱的指針。
h_addrtype –地址類型; 通常是AF_INET。
h_length – 地址的比特長度。
h_addr_list – 零位元組-主機網路地址指針。網路位元組順序。
h_addr - h_addr_list中的第一地址。
Examples
gethostbyname()成功時返回一個指向結構體 hostent 的指針,或者是個空(NULL)指針。(但是和以前不同,不設定errno,h_errno 設定錯誤信息。請看下面的herror()。但是如何使用呢?這個函式可不像它看上去那么難用。這裡是個例子:
一、
#include<stdio.h>#include<sys/socket.h>#include<sys/types.h>#include<netdb.h>#include<netinet/in.h>#include<string.h>#include<stdlib.h>int main(int argc,char* argv[]){struct hostent* h;if(argc!=2){/*檢查命令行*/fprintf(stderr,"usage:getipaddress");exit(1);}if((h=gethostbyname(argv[1]))==NULL){/*取得地址信息/herror("gethostbyname");exit(1);}printf("hostname:%s",h->h_name);printf("IPAddress:%s",inet_ntoa(*((struct in_addr*)h->h_addr)));return0;}
在使用 gethostbyname() 的時候,你不能用perror() 列印錯誤信息 (因為 errno 沒有使用),你應該調用 herror()。
相當簡單,你只是傳遞一個保存機器名的字元串 給gethostbyname(),然後從返回的數據結構 struct hostent 中獲取信息。唯一也許讓人不解的是輸出 IP 地址信息。h->h_addr 是一個 char *, 但是 inet_ntoa() 需要的是 struct in_addr。因此,我轉換 h->h_addr 成 struct in_addr *,然後得到數據。
比如“192.168.001.001”,(默認是第一塊網卡的地址)
實際儲存的情況是這樣的;
hostent->h_addr_list[0][0]= -64
hostent->h_addr_list[0][1]= -88
hostent->h_addr_list[0][2]= 1
hostent->h_addr_list[0][3]= 1
那么它是這么轉的嗎?
int i;
char buff[]="192.168.001.001";
int value[4]={0};
for(i=0;i<4;i++)
{
value[i]=(buff[4*i]-48)×100+(buff[4*i+1]-48)×10+(buff[4*i+2]-48);
}
//比如:value[1]=(49-48)×100+(57-48)×10+(50-48)x 1=192
for(i=0;i<4;i++)
{
if(value[i]>127)
{
hostent->h_addr[0][i] = value[i]-256;
}
else
{
hostent->h_addr_list[0][i] = value[i];
}
}
在引用的時候可以看下面代碼:
char *ip;
PHOSTENT hostinfo;
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
printf("%s\n",ip);
或者
for(j=0;j<h_length;j++)
{
CString addr;
if(j>0)
str+=".";
sddr.Format("%u",(unsigned int)((unsigned char*)phost->h_addr_list[occurred])[j]);
str+=addr;
}
二、
#include<winsock2.h>#include<ws2tcpip.h>#include<stdio.h>#include<windows.h>#pragmacomment(lib,"wininet.lib")intmain(intargc,char**argv){//-----------------------------------------//DeclareandinitializevariablesWSADATAwsaData;intiResult;DWORDdwError;inti=0;structhostent*remoteHost;char*host_name;structin_addraddr;char**pAlias;//Validatetheparametersif(argc!=2){printf("usage:%sipv4address\n",argv[0]);printf("or\n");printf("%shostname\n",argv[0]);printf("toreturnthehost\n");printf("%s127.0.0.1\n",argv[0]);printf("toreturntheIPaddressesforahost\n");printf("%s網址地址域名\n",argv[0]);return1;}//InitializeWinsockiResult=WSAStartup(MAKEWORD(2,2),&wsaData);if(iResult!=0){printf("WSAStartupfailed:%d\n",iResult);return1;}host_name=argv[1];//Iftheuserinputisanalphanameforthehost,usegethostbyname()//Ifnot,gethostbyaddr(assumeIPv4)if(isalpha(host_name[0])){/*hostaddressisaname*/printf("Callinggethostbynamewith%s\n",host_name);remoteHost=gethostbyname(host_name);}else{printf("Callinggethostbyaddrwith%s\n",host_name);addr.s_addr=inet_addr(host_name);if(addr.s_addr==INADDR_NONE){printf("TheIPv4addressenteredmustbealegaladdress\n");return1;}elseremoteHost=gethostbyaddr((char*)&addr,4,AF_INET);}if(remoteHost==NULL){dwError=WSAGetLastError();if(dwError!=0){if(dwError==WSAHOST_NOT_FOUND){printf("Hostnotfound\n");return1;}elseif(dwError==WSANO_DATA){printf("Nodatarecordfound\n");return1;}else{printf("Functionfailedwitherror:%ld\n",dwError);return1;}}}else{printf("Functionreturned:\n");printf("\tOfficialname:%s\n",remoteHost->h_name);for(pAlias=remoteHost->h_aliases;*pAlias!=0;pAlias++){printf("\tAlternatename#%d:%s\n",++i,*pAlias);}printf("\tAddresstype:");switch(remoteHost->h_addrtype){caseAF_INET:printf("AF_INET\n");break;caseAF_INET6:printf("AF_INET6\n");break;caseAF_NETBIOS:printf("AF_NETBIOS\n");break;default:printf("%d\n",remoteHost->h_addrtype);break;}printf("\tAddresslength:%d\n",remoteHost->h_length);if(remoteHost->h_addrtype==AF_INET){while(remoteHost->h_addr_list[i]!=0){addr.s_addr=*(u_long*)remoteHost->h_addr_list[i++];printf("\tIPv4Address#%d:%s\n",i,inet_ntoa(addr));}}elseif(remoteHost->h_addrtype==AF_INET6)printf("\tRemotehostisanIPv6address\n");}return0;}