1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
int main()
{
addrinfo hints, *a, *socket;
char conv [INET6_ADDRSTRLEN];
memset (&hints,0,sizeof hints);
hints.ai_family= AF_UNSPEC;
hints.ai_family= SOCK_STREAM; // should be ai_socktype
if (getaddrinfo ("www.google.com", "4000",&hints, &socket)==1) // any non zero value indicates failure,
{ // change to != 0
cout<<"Unable to recieve info";
exit (1);
}
void* addr;
string type;
for (a=socket;a!=NULL;a=a->ai_next)
{
if (a->ai_family == AF_INET)
{
sockaddr_in *IPv4= (struct sockaddr_in*) a->ai_addr;
addr= &(IPv4->sin_addr);
type= "IPv4";
}
else
{
sockaddr_in6 *IPv6= (struct sockaddr_in6*)a->ai_addr;
addr= &(IPv6->sin6_addr);
type= "IPv6";
}
cout << type << ": " << inet_ntop(a->ai_family, addr, conv, sizeof conv) << endl;
}
inet_ntop (a->ai_family, addr, conv, sizeof conv); // a == NULL here
cout<<type<<": "<<addr; // inet_ntop returns a char* to the string representation
freeaddrinfo (socket);
return 0;
}
|