Tutorials/C Programming Tutorial/Aliases.cpp

From ThorstensHome
Revision as of 10:09, 18 October 2008 by WikiSysop (Talk)

Jump to: navigation, search

Aliases

This simple program shows all aliases of a given host. aliases.cpp

#include <stdio.h>
#include <netdb.h>
int main(int argc, char* argv[])
{
  int i=0;
  while (gethostbyname(argv[1])->h_aliases[i] != NULL) 
  {
    printf("Alias is \"%s\"\n",gethostbyname(argv[1])->h_aliases[i]);
    i++;
  }
}

Compile, link and run it

g++ -o aliases.exe aliases.cpp && ./aliases.exe localhost

Services

Now let's make it a bit more fun:

#include <stdio.h>
#include <netdb.h>
int main(int argc, char* argv[])
{
  printf(getservbyname("www","tcp")->s_name);
  printf(getservbyname("www","tcp")->s_proto);
  printf("%i",getservbyname("www","tcp")->s_port);
}

Now we see a miracle - I want to know why ;)

tweedleburg:~/test # g++ -o aliases.exe aliases.cpp && ./aliases.ext localhost
wwwtcp20480