Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    read IP address...

    Hi friends,
    can anyone tell me how to read the IP address of the system from a C program..using ioctl calls and creating a raw socket.... something like
    socket(AF_INET, SOCK_RAW, IPPROTO_RAW) and (ioctl(fd, SIOCGIFADDR, &ifr)
    please help me if u have some other ideas or if u know something on this...

  2. #2
    Linux User
    Join Date
    Oct 2004
    what's wrong with gethostbyname()?
    What you'd get with SIOCGIFADDR is the interface address (for ethernet, MAC address) structure - not just the IP address, but quite a lot of more information.
    The Unforgiven
    Registered Linux User #358564

  3. #3
    Thanks for reply the_unforgiven,
    but if i run this program i am getting the output something like this...
    the ip address is 7012340l
    Family is 101
    the port is 2052
    string is
    so how to extract the ip address from this.... ??
    whether what i am doing is correct ???
    and please please tell me how to use gethostname()....gimme a simple piece of code to illustrate !!!
    #include <sys/ioctl.h>
    #include <net/if.h>
    #include <netinet/in.h>
    int main()
            int fd;
            struct ifreq ifr;
            struct sockaddr_in saddr;
            strcpy(ifr.ifr_name,"name of interface");
            saddr=*((struct sockaddr_in *)(&(ifr.ifr_addr))); // is the address
            printf(" the ip address is %ul\n", saddr.sin_addr.s_addr);
            printf(" Family is %d\n", saddr.sin_family);
            printf(" the port is  %d\n", saddr.sin_port);
            printf(" string is  %s\n", saddr.sin_zero);
            return 0;
            //saddr.sin_addr.s_addr is the address of the interface in integer
    thanks again...

  4. $spacer_open

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts