/* 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);
}
}