Condividi tramite


Struttura HOSTENT (winsock.h)

La struttura hostent viene usata dalle funzioni per archiviare informazioni su un determinato host, ad esempio nome host, indirizzo IPv4 e così via. Un'applicazione non deve mai tentare di modificare questa struttura o di liberare uno dei relativi componenti. Inoltre, solo una copia della struttura hostent viene allocata per thread e un'applicazione deve quindi copiare tutte le informazioni necessarie prima di emettere qualsiasi altra chiamata API Windows Sockets.

Sintassi

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

Members

h_name

Nome ufficiale dell'host (PC). Se si usa il sistema di risoluzione DNS o simile, si tratta del nome di dominio completo (FQDN) che ha causato la restituzione di una risposta dal server. Se si usa un file host locale, si tratta della prima voce dopo l'indirizzo IPv4.

h_aliases

Matrice con terminazione NULL di nomi alternativi.

h_addrtype

Tipo di indirizzo restituito.

h_length

Lunghezza, in byte, di ogni indirizzo.

h_addr_list

Elenco con terminazione NULL di indirizzi per l'host. Gli indirizzi vengono restituiti nell'ordine di byte di rete. La macro h_addr è definita per h_addr_list[0] la compatibilità con il software precedente.

Commenti

Le funzioni gethostbyaddr e gethostbyname restituiscono un puntatore a una struttura hostent , una struttura allocata da Windows Sockets. La struttura hostent contiene i risultati di una ricerca riuscita dell'host specificato nel parametro name .

La memoria per la struttura hostent restituita dalle funzioni gethostbyaddr e gethostbyname viene allocata internamente dalla DLL Winsock dall'archiviazione locale del thread. Solo una singola struttura hostent viene allocata e usata, indipendentemente dal numero di volte in cui vengono chiamate le funzioni gethostbyaddr o gethostbyname nel thread. La struttura hostent restituita deve essere copiata in un buffer dell'applicazione se devono essere effettuate chiamate aggiuntive alle funzioni gethostbyaddr o gethostbyname nello stesso thread. In caso contrario, il valore restituito verrà sovrascritto dalle successive chiamate gethostbyaddr o gethostbyname nello stesso thread. La memoria interna allocata per la struttura hostent restituita viene rilasciata dalla DLL Winsock quando il thread viene chiuso.

Un'applicazione non deve provare a rilasciare la memoria usata dalla struttura hostent restituita. L'applicazione non deve mai tentare di modificare questa struttura o di liberare uno dei relativi componenti. Inoltre, solo una copia di questa struttura viene allocata per thread, quindi l'applicazione deve copiare tutte le informazioni necessarie prima di inviare qualsiasi altra chiamata di funzione a gethostbyaddr o gethostbyname.

Esempio

Negli esempi seguenti viene illustrato l'uso della struttura hostent con la funzione 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;
}

Requisiti

Requisito Valore
Client minimo supportato Windows 2000 Professional [solo app desktop]
Server minimo supportato Windows 2000 Server [solo app desktop]
Intestazione winsock.h (include Winsock2.h)

Vedi anche

GetAddrInfoEx

GetAddrInfoW

addrinfo

addrinfoW

getaddrinfo

gethostbyaddr

gethostbyname