Name

gethostbyname, gethostbyaddr
- get network host entry

Library

libc.lib

Synopsis

  #include <netdb.h>
  struct hostent * gethostbyname (const char *name);
  struct hostent * gethostbyaddr (const char *addr, int len, int type);

Return values

gethostbyname, and gethostbyaddr returns NULL if an error occurs or else will return a valid hostent structure

Detailed description

The

The gethostbyname, and gethostbyaddr functions each return a pointer to an object with the following structure describing an internet host referenced by name or by address, respectively.

The name argument passed to gethostbyname should point to a NUL -terminated hostname. The addr argument passed to gethostbyaddr should point to an address which is len bytes long, in binary form (i.e., not an IP address in human readable ASCII form). The type argument specifies the address family (e.g. AF_INET, etc.) of this address.

The structure returned contains either the information obtained from the name server,

struct  hostent {
        char    *h_name;        /* official name of host */
        char    **h_aliases;    /* alias list */
        int     h_addrtype;     /* host address type */
        int     h_length;       /* length of address */
        char    **h_addr_list;  /* list of addresses from name server */
};
#define h_addr  h_addr_list[0]  /* address, for backward compatibility */

         

The members of this structure are:

h_name Official name of the host.
h_aliases A NULL -terminated array of alternate names for the host.
h_addrtype The type of address being returned; usually AF_INET.
h_length The length, in bytes, of the address.
h_addr_list
  A NULL -terminated array of network addresses for the host. Host addresses are returned in network byte order.
h_addr The first address in h_addr_list; this is for backward compatibility.

When using the nameserver, gethostbyname will search for the named host in the current domain and its parents unless the name ends in a dot.

getaddrinfo and getnameinfo functions are preferred over the gethostbyname, and gethostbyaddr functions.


Examples

#include<stdio.h>
#include<arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
Int main()
{
struct hostent *hp = 0;
char *test_url=www.google.com:
hp = gethostbyname(test_url);
if(hp==NULL)
printf(“gethostbyname failed”):
else
printf(“gethostbyname passed”);
return 0;
}

         

Output

Gethostbyname passed

         

#include<stdio.h>
#include<arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#define urlsize 50
Int main()
{
struct hostent *hp = 0;
char addr[256];
unsigned long  test_addr;
strcpy(addr,” 147.243.3.83”);
test_addr=inet_addr(addr);
struct hostent *hp;
hp=gethostbyaddr((const char *)&test_addr,sizeof(test_addr),AF_INET);
if(hp)
printf(“DNS query resolved”);
else
printf(“gethostbyaddr failed”);
return 0;
}

         

Diagnostics

Error return status from gethostbyname and gethostbyaddr is indicated by return of a NULL pointer. The external integer h_errno may then be checked to see whether this is a temporary failure or an invalid or unknown host. The routine If its argument string is non- NULL, it is printed, followed by a colon and a space. The error message is printed with a trailing newline.

The variable h_errno can have the following values:

TRY_AGAIN This is usually a temporary error and means that the local server did not receive a response from an authoritative server. A retry at some later time may succeed.
NO_RECOVERY Some unexpected server failure was encountered. This is a non-recoverable error.


See also

getaddrinfo, getnameinfo

Caveat


Bugs

These functions use a thread-specific data storage; if the data is needed for future use, it should be copied before any subsequent calls overwrite it.

Though these functions are thread-safe, still it is recommended to use the getaddrinfo family of functions, instead.

Only the Internet address format is currently understood.


Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top