1

I am trying to write a client-server program for ipv6. I am facing issue when I am trying to call getaddrinfo function. It returns -2 (getaddrinfo: Name or service not known).

This works fine on Windows system, but fails on Linux.

$ ifconfig |grep inet6
inet6 fe80::e41b:2c23:a286:77a  prefixlen 64  scopeid 0x20<link>
inet6 ::1  prefixlen 128  scopeid 0x10<host>

ifconfig says ipv6 is enabled. Even i can ping6 this ip from another host.

#include <iostream>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>

int main(int argc, char** argv)
{
    addrinfo hints = {}, *results, *res;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET6;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    int m = getaddrinfo(argv[1], "2019", &hints, &results);
    if (m < 0)
    {
        std::cout << "getaddrinfo : " << gai_strerror(m) << std::endl;
        return 1;
    }

    for(res = results; res != NULL; res = res->ai_next)
    {
        char straddr[INET6_ADDRSTRLEN] = {};
        if (inet_ntop(res->ai_family, &(reinterpret_cast<sockaddr_in6*>(res->ai_addr)->sin6_addr), straddr, sizeof(straddr)))
            std::cout << "IP - " << straddr << "\n";
        else
            std::cout << "inet_ntop error";
    }

    freeaddrinfo(results);

    return 0;
}

It is expected to print the ip in v6 format but it prints getaddrinfo: Name or service not known

kaps
  • 186
  • 4
  • 18

0 Answers0