gethostbyname() — Get a host entry by name
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
XPG4.2 |
both |
Format
#define _XOPEN_SOURCE_EXTENDED 1
#include <netdb.h>
extern int h_errno;
struct hostent *gethostbyname(const char *name);
#define _OE_SOCKETS
#include <netdb.h>
struct hostent *gethostbyname(char *name);
General description
- Parameter
- Description
- name
- The name of the host.
The gethostbyname() call returns a pointer to a hostent structure for the host name specified on the call.
gethostent(), gethostbyaddr(), and gethostbyname() all use the same static area to return the hostent structure. This static area is only valid until the next one of these functions is called on the same thread.
If you want gethostbyname() to bypass the name server and instead resolve the host name using the local host tables, you must define the RESOLVE_VIA_LOOKUP symbol before including any sockets-related include files in your source program.
gethostent(), gethostbyaddr(), and gethostbyname() all use the same static area to return the HOSTENT structure. This static area is only valid until the next one of these functions is called on the same thread.
- Element
- Description
- h_addr_list
- A pointer to a NULL-terminated list of host network addresses.
- h_addrtype
- The type of address returned; currently, it is always set to AF_INET.
- h_aliases
- A zero-terminated array of alternative names for the host.
- h_length
- The length of the address in bytes.
- h_name
- The official name of the host.
int *__h_errno(void);
Also use this function when you invoke gethostbyname() in a DLL. This function returns a pointer to a thread-specific value for the h_errno variable.
Returned value
The return value points to static data that is overwritten by subsequent calls. A pointer to a hostent structure indicates success. A NULL pointer indicates an error or End Of File (EOF).
- Error Code
- Description
- HOST_NOT_FOUND
- No such host is known.
- NO_DATA
- The server recognized the request and the name but no address is available. Another type of request to the name server might return an answer.
- NO_RECOVERY
- An unexpected server failure occurred from which there is no recovery.
- TRY_AGAIN
- A temporary error such as no response from a server, indicating the information is not available now but may be at a later time.