[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Technical Reference: Communications, Volume 2

gethostbyname_r Subroutine


Gets network host entry by name.


Standard C Library (libc.a)


#include netdb.h>
int gethostbyname_r(Name, Htent, Ht_data)

const char *Name, struct hostent *Htent, struct hostent_data *Ht_data;


This function internally calls the gethostbyname subroutine and stores the value returned by the gethostbyname subroutine to the hostent structure.


Name Points to the host name (which is a constant).
Htent Points to a hostent structure in which the return value of the gethostbyname subroutine is stored.
Ht_data Points to a hostent_data structure.

Return Values

The function returns a 0 if successful and a -1 if unsuccessful.


The return value of the gethostbyname subroutine points to static data that is overwritten by subsequent calls. This data must be copied at every call to be saved for use by subsequent calls. The gethostbyname_r subroutine solves this problem.

If the Name parameter is an IP address, this subroutine searches for a machine with that IP address as a name. Because of this, use the gethostbyaddr_r subroutine instead of the gethostbyname_r subroutine if the Name parameter is an IP address.

Error Codes

The gethostbyname_r subroutine is unsuccessful if any of the following errors occurs:

HOST_NOT_FOUND The host specified by the Name parameter was not found.
TRY_AGAIN The local server did not receive a response from an authoritative server. Try again later.
NO_RECOVERY An unrecoverable error occurred.
NO_ADDRESS The requested Name is valid but does not have an Internet address at the name server.
SERVICE_UNAVAILABLE None of the name services specified are running or available.
EINVAL The hostent pointer is NULL.


/etc/hosts Contains the host name data base.
/etc/resolv.conf Contains the name server and domain name.
/etc/netsvc.conf Contains the name services ordering.
/usr/include/netdb.h Contains the network database structure.

Related Information

endhostent_r Subroutine, gethostbyaddr_r Subroutine, gethostent_r Subroutine, and sethostent_r Subroutine.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]