1
/* program to print the IP address of the Host*/
------------------------------------------------

I am trying to print the Host IP address. when I execute the following program I am getting loop back address that is 127.0.0.1. What should I change to get the actual IP address.

# include <stdio.h>
# include <stdlib.h>
# include <arpa/inet.h>
# include <string.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <netdb.h>
# include <netinet/in.h>

int main ()     {

        void *addr;
        char ipstr[INET6_ADDRSTRLEN];
        int rv;
        struct addrinfo hints, *res, *p;

        memset ( &hints, 0, sizeof hints );
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
        //hints.ai_flags = AI_ADDRCONFIG;
        hints.ai_flags = AI_PASSIVE;

        if ((rv = getaddrinfo(NULL ,"3490"  , &hints, &res)) != 0) {
                fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
                return 1;
        }

        for(p = res;p != NULL; p = p->ai_next) {

                struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
                addr = &(ipv4->sin_addr);
                inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
                printf(" %s\n",  ipstr);
        }

}
MeNa
  • 1,467
  • 9
  • 23
user3035481
  • 309
  • 2
  • 3
  • 7
  • The loopback address *is* an actual address. If you want to get the address of conencted *hardware* interfaces, then you have to resort to platform-specific code. There are many example if you just search a little. – Some programmer dude Nov 26 '13 at 08:53
  • 1
    Oh and by the way, what if you get an IPv6 address in the list? Then `p->ai_addr` won't be a `sockaddr_in` pointer and so accessing `ipv4->sin_addr` is undefined behavior. – Some programmer dude Nov 26 '13 at 08:54
  • 1
    You can use `getifaddrs()` to get the IP addresses of the local interfaces, see (for example) here: http://stackoverflow.com/a/12883978/1187415. – Martin R Nov 26 '13 at 08:56
  • could you please try this link: http://www.geekpage.jp/en/programming/linux-network/get-ipaddr.php Hope this helps!!! – Srini Nov 26 '13 at 09:04

1 Answers1

0

As long as your development machine is not connected to a network you'll get the loopback address (which is perfectly valid for local network, of course). As soon as you connect the machine to the network you can determine the "right" address. ipconfig, btw, behaves the same.

If you call getaddrinfo with NULL for the node name (first parameter) it is the same as when you call it with "localhost". You'll get the loopback IP. However, if you use the "real" hostname, you'll get the "real" ip.

JeffRSon
  • 10,404
  • 4
  • 26
  • 51
  • My machine is in network. when I do ifconfig, I am getting two IP addresses one is 172.28.3.225( actual IP ) and another one is loopback address that is 127.0.0.1.what should I do to get the 172.28.3.225 – user3035481 Nov 27 '13 at 05:38
  • Sure you can, why not? – JeffRSon Nov 28 '13 at 09:09