ip address using getaddrinfo()

This is a discussion on ip address using getaddrinfo() within the C Programming forums, part of the General Programming Boards category; hello, i was able to get ip address using gethostname and gethostbyname functions but since they are now superseded by ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    38

    ip address using getaddrinfo()

    hello,

    i was able to get ip address using gethostname and gethostbyname functions but since they are now superseded by getaddrinfo() i wanted to get ip address using this function.

    here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    
    int main(int argc, char **argv) { 
    
    
    int status; 
    struct addrinfo hints, *p; 
    struct addrinfo *servinfo; 
    char ipstr[INET_ADDRSTRLEN];
    char hostname[128];
    
    
    memset(&hints, 0, sizeof hints); 
    
    
    hints.ai_family   = AF_UNSPEC;    
    hints.ai_socktype = SOCK_STREAM;  
    hints.ai_flags    = AI_PASSIVE;  
    
    
    gethostname(hostname, 128);
    
    
    if ((status = getaddrinfo(hostname, NULL, &hints, &servinfo)) == -1) { 
        fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); 
        exit(1); 
    }       
    
    
    for (p=servinfo; p!=NULL; p=p->ai_next) { 
        struct in_addr  *addr;  
        if (p->ai_family == AF_INET) { 
            struct sockaddr_in *ipv = (struct sockaddr_in *)p->ai_addr; 
            addr = &(ipv->sin_addr);  
        } 
        else { 
            struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr; 
            addr = (struct in_addr *) &(ipv6->sin6_addr); 
        }
            inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr); 
     
    } 
      
        printf("Address: %s\n", ipstr);
        freeaddrinfo(servinfo); 
    
    
    return 0;
    
    
     }
    let me know if i am doing anything wrong. the ip address i am getting is 127.0.1.1 (not 127.0.0.1)/ i know 127.0.0.1 is the localhost ip address and not the actual ip address but i do not know what 127.0.1.1 is referring to. the ip address that i was expecting was: 192.168.1.107

    wud really appreciate some help

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    127.0.1.1 is also a loopback address. The whole 127.0.0.0/8 network points to the local host.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    38
    so is there any other way of getting my actual ip address from my code?? i do not know what i am missing!!

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    You can look into the getifaddrs function. It's not standard (not in POSIX) but it can be found on many *nix systems.

    I can't say what's available in the Windows world if that's what you need.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    38
    nope, i am in the linux world right now!!

    just one ques: i want to implement a client and server program and want to run client and server one two separate computers. will this loopback address be an issue when the client is trying send data to server?? will the server still be able to receive data or send data to the client?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getaddrinfo() can't resolve hostname
    By chaos5687 in forum C Programming
    Replies: 8
    Last Post: 01-12-2011, 09:15 PM
  2. is getaddrinfo really necessary?
    By sunjayc99 in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-28-2008, 03:14 PM
  3. Why create a wrapper fo getaddrinfo()
    By Overworked_PhD in forum Linux Programming
    Replies: 2
    Last Post: 11-10-2007, 12:37 AM
  4. getaddrinfo()
    By Cactus_Hugger in forum Windows Programming
    Replies: 1
    Last Post: 07-24-2006, 02:41 AM
  5. using gethostbyname , getaddrinfo & WSAAsyncGetHostByName
    By hanhao in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-04-2004, 12:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21