获取本地IP gethostname和gethostbyname

来源:互联网 发布:淘宝店卖衣服去哪进货 编辑:程序博客网 时间:2024/06/06 02:00

    #include <netdb.h>    #include <sys/socket.h>    #include <unistd.h>      #include <sys/types.h>    #include <netdb.h>    #include <netinet/in.h>      #include <stdlib.h>     #include <netinet/in.h>       #include <arpa/inet.h>     #include <stdio.h>    struct hostent *gethostbyname(const char *name);    //这个函数的传入值是域名或者主机名,例如"www.google.cn"等等。传出值,是一个hostent的结构。如果函数调用失败,将返回NULL。    struct hostent    {        char    *h_name;                        char    **h_aliases;         int     h_addrtype;        int     h_length;        char    **h_addr_list;         #define h_addr h_addr_list[0]     };     hostent->h_name    表示的是主机的规范名。例如www.google.com的规范名其实是www.l.google.com。    hostent->h_aliases    表示的是主机的别名.www.google.com就是google他自己的别名。有的时候,有的主机可能有好几个别名,这些,其实都是为了易于用户记忆而为自己的网站多取的名字。    hostent->h_addrtype         表示的是主机ip地址的类型,到底是ipv4(AF_INET),还是pv6(AF_INET6)    hostent->h_length           表示的是主机ip地址的长度    hostent->h_addr_lisst     表示的是主机的ip地址,注意,这个是以网络字节序存储的。千万不要直接用printf带%s参数来打这个东西,会有问题的哇。所以到真正需要打印出这个IP的话,需要调用inet_ntop()。    const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) :    这个函数,是将类型为af的网络地址结构src,转换成主机序的字符串形式,存放在长度为cnt的字符串中。返回指向dst的一个指针。如果函数调用错误,返回值是NULL。#include <netdb.h>#include <sys/socket.h>#include <stdio.h>int main(int argc, char **argv){    char   *ptr, **pptr;    struct hostent *hptr;    char   str[32];    ptr = argv[1];    if((hptr = gethostbyname(ptr)) == NULL)    {        printf(" gethostbyname error for host:%s\n", ptr);        return 0;     }    printf("official hostname:%s\n",hptr->h_name);    for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)        printf(" alias:%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)));            printf(" first address: %s\n",                        inet_ntop(hptr->h_addrtype, hptr->h_addr, str, sizeof(str)));        break;        default:            printf("unknown address type\n");        break;    }    return 0;}

int CClientSocket::GetLocalHostName(CString &sHostName)//获得本地计算机名称{char szHostName[256];int nRetCode;nRetCode=gethostname(szHostName,sizeof(szHostName));if(nRetCode!=0){//产生错误sHostName=_T("没有取得");return GetLastError();}sHostName=szHostName;return 0;}int CClientSocket::GetIpAddress(const CString &sHostName, CString &sIpAddress)//获得本地IP{struct hostent FAR * lpHostEnt=gethostbyname(sHostName);if(lpHostEnt==NULL){//产生错误sIpAddress=_T("");return GetLastError();}//获取IPLPSTR lpAddr=lpHostEnt->h_addr_list[0];if(lpAddr){struct in_addr inAddr;memmove(&inAddr,lpAddr,4);//转换为标准格式sIpAddress=inet_ntoa(inAddr);if(sIpAddress.IsEmpty())sIpAddress=_T("没有取得");}return 0;}int CClientSocket::GetIpAddress(const CString &sHostName, BYTE &f0,BYTE &f1,BYTE &f2,BYTE &f3)//获得本地IP{struct hostent FAR * lpHostEnt=gethostbyname(sHostName);if(lpHostEnt==NULL){//产生错误f0=f1=f2=f3=0;return GetLastError();}//获取IPLPSTR lpAddr=lpHostEnt->h_addr_list[0];if(lpAddr){struct in_addr inAddr;memmove(&inAddr,lpAddr,4);f0=inAddr.S_un.S_un_b.s_b1;f1=inAddr.S_un.S_un_b.s_b2;f2=inAddr.S_un.S_un_b.s_b3;f3=inAddr.S_un.S_un_b.s_b4;}return 0;}


0 0
原创粉丝点击