Open In App

C Program to display hostname and IP address

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C program to display hostname
// and IP address
#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>
 
// Returns hostname for the local computer
void checkHostName(int hostname)
{
    if (hostname == -1)
    {
        perror("gethostname");
        exit(1);
    }
}
 
// Returns host information corresponding to host name
void checkHostEntry(struct hostent * hostentry)
{
    if (hostentry == NULL)
    {
        perror("gethostbyname");
        exit(1);
    }
}
 
// Converts space-delimited IPv4 addresses
// to dotted-decimal format
void checkIPbuffer(char *IPbuffer)
{
    if (NULL == IPbuffer)
    {
        perror("inet_ntoa");
        exit(1);
    }
}
 
// Driver code
int main()
{
    char hostbuffer[256];
    char *IPbuffer;
    struct hostent *host_entry;
    int hostname;
 
    // To retrieve hostname
    hostname = gethostname(hostbuffer, sizeof(hostbuffer));
    checkHostName(hostname);
 
    // To retrieve host information
    host_entry = gethostbyname(hostbuffer);
    checkHostEntry(host_entry);
 
    // To convert an Internet network
    // address into ASCII string
    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++




// C++ program to display hostname
// and IP address
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
  
// Function use
int main()
{
    char hostbuffer[256];
    struct hostent *host_entry;
    int hostname;
    struct in_addr **addr_list;
 
    // retrieve hostname
    hostname = gethostname(hostbuffer, sizeof(hostbuffer));
    if (hostname == -1) {
        perror("gethostname error");
        exit(1);
    }
    printf("Hostname: %s\n", hostbuffer);
 
    // Retrieve IP addresses
    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)



Last Updated : 18 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads