Facebook Twitter Instagram
    WiredRevolution.com
    • Home
    • About
    • Contact Us
    • Essential Linux Commands
    • Sitemap
    Facebook Twitter Instagram
    WiredRevolution.com
    C

    Display hostname and IP address in C

    RyanBy RyanOctober 18, 2008Updated:October 30, 20084 Comments1 Min Read
    Facebook Twitter Pinterest LinkedIn Reddit WhatsApp Email

    Here is a good way to determine the hostname and IP address of the local machine in C.

    You first have to grab the hostname with gethostname().

    char hostbuf[256];
    gethostname(hostbuf,sizeof(hostbuf));
    

    Take the hostname and use it to grab the hostent struct with gethostbyname().

    struct hostent *hostentry;
    hostentry = gethostbyname(hostbuf);
    

    Finally you have to take the hostent that is returned and pull out the IP address. It is in network byte order, so you have to convert it to a string with inet_ntoa().

    char * ipbuf;
    ipbuf = inet_ntoa(*((struct in_addr *)hostentry->h_addr_list[0]));
    

    Put all this together into a working programs.

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    
    int main()
    {
            char hostbuf[256];
            char * ipbuf;
            struct hostent *hostentry;
            int ret;
    
            ret = gethostname(hostbuf,sizeof(hostbuf));
    
            if(-1 == ret){
                    perror("gethostname");
                    exit(1);
            }
    
            hostentry = gethostbyname(hostbuf);
    
            if(NULL == hostentry){
                    perror("gethostbyname");
                    exit(1);
            }
    
            ipbuf = inet_ntoa(*((struct in_addr *)hostentry->h_addr_list[0]));
    
            if(NULL == ipbuf){
                    perror("inet_ntoa");
                    exit(1);
            }
    
            printf("Hostname: %s Host IP: %s\n", hostbuf, ipbuf);
    
            return 0;
    }
    
    
    Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Email
    Previous ArticleView dynamic library dependencies with ldd
    Next Article Customize the BASH PS1 command prompt

    Related Posts

    Clone a VirtualBox Guest VM

    Change hostname on RedHat

    How to SSH into Ubuntu LiveCD

    Most Commented
    March 12, 2009

    Fix blue tinted video in Ubuntu

    September 10, 2010

    Setup SSH access between VirtualBox Host and Guest VMs

    March 8, 2011

    Install GNOME Shell in Ubuntu 10.10 Maverick

    April 4, 2009

    Setup the PS3 Bluetooth Controller on Ubuntu

    October 22, 2008

    How to correctly use LD_LIBRARY_PATH

    Recent Comments
    • Execute command on linux virtual machine (or server) from windows commandline on Setup SSH access between VirtualBox Host and Guest VMs
    • Solved: How to SSH to a VirtualBox guest externally through a host? - Daily Developer Blog on Setup SSH access between VirtualBox Host and Guest VMs
    • How to SSH to a VirtualBox guest externally through a host? [closed] – Code D3 on Setup SSH access between VirtualBox Host and Guest VMs
    • How to copy and paste from VirtualBox? [duplicate] on Setup SSH access between VirtualBox Host and Guest VMs
    • Jackie Laguna on Fix OpenGL: ChoosePixelFormat SketchUp error in WINE
    © 2025 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.