gtpc1m3o | Transmission Control Protocol/Internet Protocol |
The gethostbyaddr function returns information about a host specified by an Internet Protocol (IP) address.
Format
#include <netdb.h> struct hostent *gethostbyaddr(char *addr, int addrlen, int domain);
Normal Return
This function returns a pointer to a hostent structure for the host name specified on the call. The netdb.h header file defines the hostent structure, which contains the following elements:
Error Return
A NULL pointer indicates an error. The value of h_errno indicates the specific error.
Programming Considerations
The gethostbyaddr function tries to resolve the host Internet address through a name server if one is present.
Examples
The following example obtains the host name associated with a given IP address.
#include <types.h> #include <socket.h> #include <netdb.h>
·
·
·
struct hostent *h; struct sockaddr_in sin; char domain[512]; sin.sin_addr.s_addr=gethostid(); h = gethostbyaddr((char *)&sin.sin_addr.s_addr, sizeof(struct in_addr), AF_INET); if (h!=(struct hostent *)0) { strcpy(domain,h->h_name); printf("gethostbyaddr was successful\n"); } else printf("gethostbyaddr failed\n");
Related Information