hostENT 结构 (winsock.h)

函数使用 hostent 结构来存储有关给定主机的信息,例如主机名、IPv4 地址等。 应用程序绝不应尝试修改此结构或释放其任何组件。 此外,每个线程只分配 主机结构的一 个副本,因此,应用程序应在发出任何其他 Windows 套接字 API 调用之前复制它所需的任何信息。

语法

typedef struct hostent {
  char  *h_name;
  char  **h_aliases;
  short h_addrtype;
  short h_length;
  char  **h_addr_list;
} HOSTENT, *PHOSTENT, *LPHOSTENT;

成员

h_name

主机 (电脑) 的官方名称。 如果使用 DNS 或类似的解析系统,则完全限定的域名 (FQDN) 导致服务器返回回复。 如果使用本地主机文件,则它是 IPv4 地址后的第一个条目。

h_aliases

NULL 结尾的备用名称数组。

h_addrtype

要返回的地址类型。

h_length

每个地址的长度(以字节为单位)。

h_addr_list

主机的以 NULL 结尾的地址列表。 地址按网络字节顺序返回。 宏 h_addr 定义为 h_addr_list[0] ,以便与旧版软件兼容。

注解

gethostbyaddrgethostbyname 函数返回指向主机结构的指针,该结构由 Windows 套接字分配。 hostent 结构包含成功搜索 name 参数中指定的主机的结果。

gethostbyaddrgethostbyname 函数返回的 hostent 结构的内存由 Winsock DLL 从线程本地存储内部分配。 无论在线程上调用 gethostbyaddrgethostbyname 函数多少次,都只分配和使用一个 hostent 结构。 如果要在同一线程上对 gethostbyaddrgethostbyname 函数进行其他调用,则必须将返回的 hostent 结构复制到应用程序缓冲区。 否则,返回值将被同一线程上的后续 gethostbyaddrgethostbyname 调用覆盖。 为返回的 主机 结构分配的内部内存在线程退出时由 Winsock DLL 释放。

应用程序不应尝试释放返回的 hostent 结构使用的内存。 应用程序不得尝试修改此结构或释放其任何组件。 此外,每个线程只分配此结构的一个副本,因此应用程序应在向 gethostbyaddrgethostbyname 发出任何其他函数调用之前复制它所需的任何信息。

示例

以下示例演示如何将 hostent 结构与 gethostbyname 函数结合使用。

#define WIN32_LEAN_AND_MEAN

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

// Need to link with Ws2_32.lib
#pragma comment(lib, "ws2_32.lib")

int main(int argc, char **argv)
{

    //-----------------------------------------
    // Declare and initialize variables
    WSADATA wsaData;
    int iResult;

    DWORD dwError;
    int i = 0;

    struct hostent *remoteHost;
    char *host_name;
    struct in_addr addr;

    char **pAlias;

    // Validate the parameters
    if (argc != 2) {
        printf("usage: %s ipv4address\n", argv[0]);
        printf(" or\n");
        printf("       %s hostname\n", argv[0]);
        printf("  to return the host\n");
        printf("       %s 127.0.0.1\n", argv[0]);
        printf("  to return the IP addresses for a host\n");
        printf("       %s www.contoso.com\n", argv[0]);
        return 1;
    }
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    host_name = argv[1];

// If the user input is an alpha name for the host, use gethostbyname()
// If not, get host by addr (assume IPv4)
    if (isalpha(host_name[0])) {        /* host address is a name */
        printf("Calling gethostbyname with %s\n", host_name);
        remoteHost = gethostbyname(host_name);
    } else {
        printf("Calling gethostbyaddr with %s\n", host_name);
        addr.s_addr = inet_addr(host_name);
        if (addr.s_addr == INADDR_NONE) {
            printf("The IPv4 address entered must be a legal address\n");
            return 1;
        } else
            remoteHost = gethostbyaddr((char *) &addr, 4, AF_INET);
    }

    if (remoteHost == NULL) {
        dwError = WSAGetLastError();
        if (dwError != 0) {
            if (dwError == WSAHOST_NOT_FOUND) {
                printf("Host not found\n");
                return 1;
            } else if (dwError == WSANO_DATA) {
                printf("No data record found\n");
                return 1;
            } else {
                printf("Function failed with error: %ld\n", dwError);
                return 1;
            }
        }
    } else {
        printf("Function returned:\n");
        printf("\tOfficial name: %s\n", remoteHost->h_name);
        for (pAlias = remoteHost->h_aliases; *pAlias != 0; pAlias++) {
            printf("\tAlternate name #%d: %s\n", ++i, *pAlias);
        }
        printf("\tAddress type: ");
        switch (remoteHost->h_addrtype) {
        case AF_INET:
            printf("AF_INET\n");
            break;
        case AF_INET6:
            printf("AF_INET6\n");
            break;
        case AF_NETBIOS:
            printf("AF_NETBIOS\n");
            break;
        default:
            printf(" %d\n", remoteHost->h_addrtype);
            break;
        }
        printf("\tAddress length: %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("\tIPv4 Address #%d: %s\n", i, inet_ntoa(addr));
            }
        } else if (remoteHost->h_addrtype == AF_INET6)
            printf("\tRemotehost is an IPv6 address\n");
    }

    return 0;
}

要求

要求
最低受支持的客户端 Windows 2000 Professional [仅限桌面应用]
最低受支持的服务器 Windows 2000 Server [仅限桌面应用]
标头 winsock.h (包括 Winsock2.h)

另请参阅

GetAddrInfoEx

GetAddrInfoW

addrinfo

addrinfoW

getaddrinfo

gethostbyaddr

gethostbyname