C Program to display hostname and IP address
Last Updated :
18 Apr, 2023
Method 1:
There are many ways to find Hostname and IP address of a local machine. Here is a simple method to find hostname and IP address using C program. We will be using the following functions :- gethostname() : The gethostname function retrieves the standard host name for the local computer. gethostbyname() : The gethostbyname function retrieves host information corresponding to a host name from a host database. inet_ntoa() : The inet_ntoa function converts an (Ipv4) Internet network address into an ASCII string in Internet standard dotted-decimal format.
C/C++
C++
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void checkHostName(int hostname)
{
if (hostname == -1)
{
perror("gethostname");
exit(1);
}
}
void checkHostEntry(struct hostent * hostentry)
{
if (hostentry == NULL)
{
perror("gethostbyname");
exit(1);
}
}
void checkIPbuffer(char *IPbuffer)
{
if (NULL == IPbuffer)
{
perror("inet_ntoa");
exit(1);
}
}
int main()
{
char hostbuffer[256];
char *IPbuffer;
struct hostent *host_entry;
int hostname;
hostname = gethostname(hostbuffer, sizeof(hostbuffer));
checkHostName(hostname);
host_entry = gethostbyname(hostbuffer);
checkHostEntry(host_entry);
IPbuffer = inet_ntoa(*((struct in_addr*)
host_entry->h_addr_list[0]));
printf("Hostname: %s\n", hostbuffer);
printf("Host IP: %s", IPbuffer);
return 0;
}
|
Output
Hostname: 31811ca6b90f
Host IP: 172.17.0.4
Output:
Hostname: cContainer
Host IP: 10.98.162.101
Output varies machine to machine
Method 2:
The hostname and IP address of a local machine can be found in a variety of ways. Here is a straightforward C programme to find hostname and IP address. The following operations will be used:- gethostname(): The gethostname method returns the local computer’s default host name. gethostbyname(): This function extracts host data from a host database corresponding to a host name. The inet_ntoa function transforms a (Ipv4) Internet network address into an ASCII string using the dotted-decimal Internet standard.
C++
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
int main()
{
char hostbuffer[256];
struct hostent *host_entry;
int hostname;
struct in_addr **addr_list;
hostname = gethostname(hostbuffer, sizeof(hostbuffer));
if (hostname == -1) {
perror("gethostname error");
exit(1);
}
printf("Hostname: %s\n", hostbuffer);
host_entry = gethostbyname(hostbuffer);
if (host_entry == NULL) {
perror("gethostbyname error");
exit(1);
}
addr_list = (struct in_addr **)host_entry->h_addr_list;
for (int i = 0; addr_list[i] != NULL; i++) {
printf("IP address %d: %s\n", i+1, inet_ntoa(*addr_list[i]));
}
return 0;
}
|
Output
Hostname: bd15a44cb17c
IP address 1: 172.17.0.3
Time Complexity : O(n)
Auxilitary Space Complexity : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...