Unix Network Programming Manual
Prev Page Next Page

Getting Host Data

In various situations, you may find that you have one piece of information about a host, but you need others. For example, a host typically has an Internet address, an Internet name and possibly alias names. If you were writing a client process, you might prefer to have the user specify the name of the server machine to use and then find the address. For example, if you use ftp, it is much more convenient to use ftp bozo.clown.edu than ftp 123.32.184.19. This conversion can be accomplished with the gethostbyname function. If you pass the name of a host, it returns a pointer to a structure with the following form: For example, if you execute the following call in a program, and print the host structure, you will get: Note that there could be multiple aliases and addresses. h_addrtype = 2 indicates the AF_UNIX family.

Similarly, if you have the address of a machine, but not the name, such as after accepting a connection or receiving a datagram, you can find the name by using gethostbyaddr Where the address is passed as a character string of length len and the type is specifically given. For example the following code finds out what machine has just made a connection with the server. gethostbyaddr is designed to work for all types of addresses, so each part of the data field has to be treated in a general way, rather than as a known Internet type of address. It should be noted that the addresses are stored in the hostent structure in network byte order format, so they need not be converted before moving them to address structures, such as sockaddr_in. For example, will work to move the address into the address structure. However, it is typical to avoid data typing problems by using something like this.


Prev Page Next Page