C Program to Display Hostname and IP Address
Overview
A hostname is a distinct name or label assigned to any device connected to a computer network, while An Internet Protocol or IP address is a unique identifier that identifies a device on the Internet or in a local network. Each host has its unique IP address by which they are connected to the network. So let's implement a C program to find the name of the host and its IP address.
Introduction
A hostname is a distinct name or label assigned to any device connected to a computer network. It makes it easier to distinguish between different computers or devices that are linked to the Internet, a network, or both. A hostname can be alphabetic or alphanumeric, and it might include a few symbols used to identify a specific network node or device. The syntax and requirements of a hostname differ depending on the naming system in use. For example, when used in the Domain Name System (DNS) or on the Internet, a hostname must include the top-level domain name (TLD) and be between one and 63 characters long.
An Internet Protocol or IP address is a unique identifier that identifies a device on the Internet or in a local network. It is a set of rules that governs the format of data transmitted over the Internet or a local network. Each host has its unique IP address by which they are connected to the network. In this article, we will find the IP address and the hostname using a C program.
Implementation
Function definition
Function | Description |
---|---|
gethostname() | This function specifies the standard host name for the local computer. |
gethostbyname() | This function retrieves host information from the host database corresponding to a hostname. |
inet_ntoa() | This function transforms an IPv4 Internet network address to an ASCII string in dotted decimal format. |
Example Code
Output
Explanation
Step 1. Create a character array of size one greater than the size of the maximum possible size of the hostname. Step 2. Use gethostname function to get the host's name and check if the value returned by the function is not equal to -1. Step 3. Use gethostbyname function to get the IP address. Step 4. Convert the IP address returned by the function to an ASCII string in dotted decimal format using the inet_ntoa function.
Note:- In C and C++, perror is used to print an error message to stderr.
Conclusion
- A hostname is a distinct name or label assigned to any device connected to a computer network.
- A hostname can be alphabetic or alphanumeric, and it might include a few symbols used to identify a specific network node or device.
- An Internet Protocol or IP address is a unique identifier that identifies a device on the Internet or in a local network.
- gethostname() function specifies the standard host name for the local computer.
- gethostbyname() function retrieves host information from the host database corresponding to a hostname.
- inet_ntoa() function transforms an IPv4 Internet network address to an ASCII string in dotted decimal format.