gethostname gethostbyname gethostbyaddr 获得主机相关信息

网络编程里经常需要获得主机的相关信息,下面围绕相关的函数以及用到的结构来说明。

获得主机名:
int gethostname( char FAR
*name, //[out] Pointer to a buffer that receives the local host name. int namelen //[in] Length of the buffer. );
返回0表示成功,失败返回SOCKET_ERROR,错误代码通过调用WSAGetLastError查看

根据主机名获得主机信息:
struct hostent FAR *gethostbyname( const char FAR *name );
该函数返回一个hostent指针,"hostent"是"host entry"的缩写,接下来看一下hostent的结构:
struct hostent {
char FAR * h_name; /* official name of host */ //主机名
char FAR * FAR * h_aliases; /* alias list */ //别名,一般为NULL
short h_addrtype; /* host address type */ //地址类型 一般为2,即AF_INET
short h_length; /* length of address */ //地址长度,ipv4地址为4
char FAR * FAR * h_addr_list; /* list of addresses */ //地址列表,假如有虚拟机的话,可能包含多个地址
#define h_addr h_addr_list[0] /* address, for backward compat */
};
举例说明:

   WSADATA wsaData;  
    WSAStartup(MAKEWORD(2, 2), &wsaData);

    char szHostName[MAX_PATH] = { 0 };
    gethostname(szHostName, MAX_PATH);
    int err = GetLastError();

    hostent *ptent = gethostbyname(szHostName);
    printf("%s \t %s \t %d \n", ptent->h_name, ptent->h_aliases, ptent->h_addrtype);
    int i = 0;
    while(ptent->h_addr_list[i])
    {

      //一般机器上没有虚拟机,或者只哟一块儿网卡的话,这里只输出一项,即 h_addr_list[0]

        printf("addr:%s \n",inet_ntoa(*(in_addr*)ptent->h_addr_list[i]));
        i ++;
    }

WSACleanup();

 

不过gethostbyname已经被遗弃使用了,推荐使用getaddrinfo。看一下原著:

The gethostbyname function retrieves host information corresponding to a host name from a host database.

Note  The gethostbyname function has been deprecated by the introduction of the getaddrinfo function. Developers creating Windows Sockets 2 applications are urged to use the getaddrinfo function instead of gethostbyname.

gethostbyname只能解决传入的主机名,当不知道主机名,只知道IP地址时,此函数就无能为力了,这就得用两外一个函数:
gethostbyaddr。该函数同样返回hostent *先看一下该函数定义:
struct HOSTENT FAR * gethostbyaddr(
const char FAR *addr, //[in] Pointer to an address in network byte order.
 int len, //[in] Length of the address.
 int type //[in] Type of the address, such as the AF_INET address family type (defined // as TCP, UDP, and other associated Internet protocols). Address family types and // their corresponding values are defined in the winsock2.h header file.
);

此函数的第一个参数是一个网络序的地址形式,所以要想传入第一个参数,还得调用inet_addr(),将点分十进制的地址变为网络地址
//The inet_addr function converts a string containing an (Ipv4) Internet Protocol dotted address into a proper address for the IN_ADDRstructure.
inet_addr原型:
unsigned long inet_addr( const char FAR *cp );

举例说明:

    WSADATA wsaData;  
    WSAStartup(MAKEWORD(2, 2), &wsaData);

    unsigned long ulong = inet_addr("127.0.0.1");
    hostent *P = gethostbyaddr((char *)&ulong, 4, AF_INET);
    printf("----------------------------------------------------\n");
    int   i = 0;
    printf("%s \t %s \t %d \n", P->h_name, P->h_aliases, P->h_addrtype);
    while(P->h_addr_list[i])
    {
        printf("addr :%s \n", inet_ntoa(*(in_addr *)(P->h_addr_list[i])));
        i++;
    }

WSACleanup();


不过有个前提,在使用以上任何函数之前先应该包含 #include <winsock2.h> 以及相应的lib  #pragma comment (lib, "ws2_32.lib")

 

posted on 2018-10-08 14:39  priarieNew  阅读(499)  评论(0编辑  收藏  举报

导航