C Programming

C GetHostByName Function Usage

The Domain Name System (DNS) server is considered as the foundation stone of networking since this server is used to resolve the hostnames into relevant IP addresses. An IP address is mandatory for communicating with a device over a network since it serves as an identity of that server. Therefore, it is considered very important to get any device registered with the DNS server before it can start communicating with other devices over the network.

Now, you might be thinking that why do we need to translate the hostnames into the IP addresses in the first place? We as humans are more comfortable in dealing with the hostnames since they are easy to remember because they are generally a combination of simple English words. However, when it comes to computer systems, they understand numbers well, because of which it becomes a necessity to translate a hostname into a dedicated IP address.

There is a dedicated DNS server that deals with all this. Whenever you use any browser to search for a particular host over the Internet while mentioning the hostname, the DNS server is contacted first to resolve that hostname into its relevant IP address. Afterward, that IP address is searched so that the relevant web page (the one you had asked for) can easily be displayed on your web browser.

Similarly, while programming in C, you come across such situations in which you need to get a hostname resolved into its relevant IP address. The “gethostbyname()” function of the C programming language is designed to serve this purpose. In this tutorial, we will try to learn more about this function of the C programming language. Moreover, we will also discuss in detail an example use-case of the “gethostbyname()” function in C.

Significance of the GetHostByName Function in C:

The gethostbyname() function of the C programming language is used to resolve a hostname into all those characteristics with which this host can be identified over a network, such as the associated IP address, alias name, length of the host address and its type, etc. The most common use-case of this function in C is DNS lookup, in which you can use this function to translate a hostname into its associated IP address by looking for it within the DNS server entries.

Syntax of the GetHostByName Function in C:

The correct syntax with which you can make use of the gethostbyname() function in C is as follows:

struct hostent *gethostbyname(const char *hostname)

The gethostbyname() function belongs to the “hostent” structure. This function only accepts a single argument, which is the host’s name to be resolved. If the name of the host cannot be found or is invalid, then an error message is generated as a result of calling the gethostbyname() function of the C programming language.

Usage of the GetHostByName Function in C:

For using the gethostbyname() function of C in a simple manner, you can take a look at the following C program:

This program makes use of a relatively large number of libraries that you must include so that you can conveniently use all the functions that have been used in this C program. We first created a function named “DNSLookUp” with an integer data type. This function accepts two arguments, i.e., a character pointer named “hostname” and another one named “ip.” Here, the “hostname” parameter will be passed to this function as a command-line argument once we will execute our code. The “ip” parameter simply corresponds to the character array that will hold the IP address translation of the provided hostname.

Then, we have created two pointers of the structures type “hostent” and “in_addr.” After that, we have an “if” statement to check whether the value returned by the “gethostbyname()” function is “NULL” or not. If it is “NULL” then, our program will terminate while displaying an error message. If not, then our “for” loop will be executed in which the DNS server will be looked up for the IP address against the provided hostname. If the corresponding IP address is found, then this function will return a “0” value otherwise “1”.

Then, there is our “main()” function that accepts the two command-line arguments “argc” and “argv.” Within this function, we have an “if” statement that will check whether the command-line arguments are less than two or not. If this statement is true, then an error message will be printed on the terminal, and our program will simply terminate. It means that we need two command-line arguments, i.e., the name of the executable file and the hostname, to be resolved.

After that, we created a character type pointer named “hostname” and assigned the value of our second command-line argument, i.e., the hostname, to be resolved. Then, we have created a character array of size 100 bytes that will hold the IP address corresponding to the provided hostname. After that, we have simply called the “DNSLookUp()” function with the “hostname” and “ip” parameters. Finally, a message will be printed on the terminal, the provided hostname, and its corresponding IP address.

Then, we compiled the code shown above with the command that follows:

$ gcc GetHostByName.c –o GetHostByName

Then, we executed this compiled code with the command given below. This time, we wanted to find out the IP address corresponding to the “localhost.”

$ ./GetHostByName localhost

The IP address corresponding to the “localhost” is shown in the following image:

We wanted to check the IP address corresponding to “www.google.com” to test this program further. For that, we executed the command shown below:

$ ./GetHostByName www.google.com

The IP address corresponding to “www.google.com” is shown in the following image:

Finally, we wanted to check the IP address corresponding to “linuxhint.com.” For that, we executed the command shown below:

$ ./GetHostByName linuxhint.com

The IP address corresponding to “linuxhint.com” is shown in the following image:

Conclusion:

This article was intended to enlighten you about the “gethostbyname()” function of the C programming language. We started off by giving you a detailed explanation of the hostname resolution philosophy into a relevant IP address and its significance in networking. Then, we walked you through the purpose of C’s “gethostbyname()” function by stating its importance followed by its syntax while paying particular attention to the arguments that this function accepts. Moreover, we also talked about the return type of this function. Finally, we discussed a sample use-case of the “gethostbyname()” function in C in which we wanted to resolve the given hostname into its corresponding IP address by providing that hostname as a command-line argument while running our C program. By implementing the code shared in this article, you will be able to find out the IP address associated with any given hostname, provided that the hostname is valid.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.