Interview Questions

How to finding a Machine Address?

Beginners Guide to Sockets


(Continued from previous question...)

How to finding a Machine Address?

Finding an address that can be used to connect to a remote machine is done with either of the following commands:

#include &ltnetdb.h>

struct hostent *gethostbyname(char *name)

struct hostent *gethostbyaddr(char *addr,
    int len, int type)

name contains the host name for which the IP address is needed. addr points to a structure of type in_addr and len is the size in bytes of this structure. In this discussion type is always AF_INET since the discussion is limited to use of IP addresses on the Internet.
Both calls return a pointer to a host entry structure.

This structure has the following form:

struct hostent {
char   *h_name; /*official name of host*/
char   **h_aliases;  /* alias list */
int    h_addrtype;   /* 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 */
	};

(Continued on next question...)

Other Interview Questions