Skip to content

Instantly share code, notes, and snippets.

@yifu
Created September 21, 2012 15:30
Show Gist options
  • Save yifu/3762191 to your computer and use it in GitHub Desktop.
Save yifu/3762191 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <ace/OS.h>
#include <ace/SOCK_Connector.h>
#include <ace/SOCK_Stream.h>
#ifndef NI_MAXHOST
#define NI_MAXHOST 1025
#endif
int main(void)
{
struct addrinfo * result;
struct addrinfo * res;
int error;
char hostName[1024] = "";
ACE_OS::hostname( hostName, sizeof hostName );
struct hostent *hostnameResult = ACE_OS::gethostbyname( hostName );
std::string name;
if ( hostnameResult && hostnameResult->h_name )
name = hostnameResult->h_name;
std::cout << "name [" << name << "] hostname[" << hostName << "]" << std::endl;
/* resolve the domain name into a list of addresses */
error = getaddrinfo( hostName, NULL, NULL, &result );
if (error != 0)
{
fprintf(stderr, "error in getaddrinfo: %s\n", gai_strerror(error));
return 1;
}
/* loop over all returned results and do inverse lookup */
for (res = result; res != NULL; res = res->ai_next)
{
char hostname[NI_MAXHOST] = "";
error = getnameinfo( res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST, NULL, 0, 0 );
if ( error != 0 )
{
fprintf(stderr, "error in getnameinfo: %s\n", gai_strerror(error));
continue;
}
if (*hostname)
{
printf("hostname: %s\n", hostname);
}
}
freeaddrinfo(result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment