locked
Get IPV6 address in c RRS feed

  • Question

  • Hye..

    I need to get IPv6 in c code..Previous, to get ipv4 I used gethostbyaddr..But in ipv6 I couldn't find any api..

    tq in advance if someone can help me..

    Tuesday, April 2, 2013 8:53 AM

Answers

All replies

  • Gethostbyaddr is deprecated.     You should call getaddrinfo and look for an IPV6 entry.

    The doc for getaddrinfo here has an example of how to use it to retrieve IPV6 info

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx


    • Edited by Ron Natalie Tuesday, April 2, 2013 1:49 PM
    • Proposed as answer by Elegentin Xie Tuesday, April 9, 2013 11:51 AM
    • Marked as answer by Elegentin Xie Friday, April 19, 2013 7:44 AM
    Tuesday, April 2, 2013 1:43 PM
  • Thanks..

    I'm sorry about my previous post that not stated the complete statement of my problem. My problem now, my current getaddrinfo in ipv6 read the data from parameter which is like this:

    Default IP = NULL;

    argv 4 is:

    IP =  "IPV6 address format"

    call Ip:

    getaddrinfo(IP)

    How I want to write the IPv6 address in some file exp: IP.dat first then I call in the getaddrinfo().

    It will be like this..

    funct Write IPV6 :

    IPv6address.dat

    funct Main :

    Default IP = NULL

    GetAddrInfo(read IPv6address.dat)

    In addition of my question. With this code it will retrieve IPv6 code:

    string

    strHostName = System.Net.Dns.GetHostName(); ;

         

    IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

           

    IPAddress[] addr = ipEntry.AddressList;

           

    Console.WriteLine(addr[addr.Length - 1].ToString());

           

    if (addr[0].AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)

            {

               

    Console.WriteLine(addr[0].ToString());//ipv6


            }

           

    string input = addr[0].ToString();

           

    string output = input.Substring(0, input.IndexOf('%'));

                

           

    return output;

    but how to make this on C code..

    Thanks in advance.



    • Edited by dhaisza Friday, April 5, 2013 4:36 AM
    Friday, April 5, 2013 2:18 AM
  • Then do getnameinfo.    The docs have examples there as well.

    Friday, April 5, 2013 1:05 PM
  • after getaddrinfo, should do WSAAddressToString to get IPv6 address
    • Proposed as answer by NewSD Tuesday, July 16, 2013 6:42 AM
    Tuesday, July 16, 2013 6:42 AM