| die letzten Änderungen * Seitenstruktur * Stichwortsuche :


logo_puschin.jpg

Startseite

Knowledge base



Kontakt

Impressum

Knowledge base - C

Knowledge base

9 Benutzer online Druckversion




zurück



C
Beispiel für gethostbyaddr

gethostbyaddr.c :



#define  BSD                // WIN for Winsock and BSD for BSD sockets

//------------- Include files ------------------------------------------------
#include <stdio.h>          // Needed for printf()
#include <stdlib.h>         // Needed for exit()
#include <string.h>         // Needed for memcpy() and strcpy()

#ifdef WIN
  #include <windows.h>      // Needed for all Winsock stuff
#endif
#ifdef BSD
  #include <sys/types.h>       // Needed for system defined identifiers.
  #include <netinet/in.h>      // Needed for internet address structure.
  #include <arpa/inet.h>       // Needed for "inet_ntoa".
  #include <sys/socket.h>      // Needed for socket(), bind(), etc...
  #include <fcntl.h>
  #include <netdb.h>           // Need for gethostbyaddr().
#endif

//===== Main program===========================================================

void main(int argc, char *argv[])
{

#ifdef WIN
  WORD wVersionRequested = MAKEWORD(1,1);       // Stuff for WSA functions
  WSADATA wsaData;                              // Stuff for WSA functions
#endif

  struct hostent  *host;                // Structure for gethostbyaddr()
  struct in_addr  *myaddr;              // Structure for Internetaddress
  char            ip_address[256];      // String for IP address

  if (argc != 2)
  {
    printf("need host's IP address as commmand line arguments \n");
    printf("usage is 'getname IP_address' \n");
    exit(1);
  }

#ifdef WIN
  // This stuff initializes winsock
  WSAStartup(wVersionRequested, &wsaData);
#endif

  // Copy IP address  into ip_address
  strcpy(ip_address, argv[1]);

  myaddr=(struct in_addr*)malloc(sizeof(struct in_addr));
  myaddr->s_addr=inet_addr(ip_address) ;

  // Do a gethostbyaddr() to get a pointer to struct host
  printf("Looking for host name for for '%s'... \n", ip_address);
  host = gethostbyaddr((char *) myaddr, 4 ,AF_INET);


  // Output host name if host found
  if (host == NULL)
    printf("  Host name for '%s' could not be found \n",ip_address);
  else
    printf("  Host name for '%s' is '%s' \n",ip_address, host->h_name);

#ifdef WIN
  // This stuff cleans-up winsock
  WSACleanup();
#endif

}



zurück



Knowledge base wurde zuletzt bearbeitet am 12.07.13 durch Frank

www.puschin.de
login

<body bgcolor='#FFFFFF' link='#000000' vlink='#000000' alink='#000000' text='#000000'><font face='Verdana' size='2'><strong>www.puschin.de</strong><br>Die Webseite http://www.puschin.de bietet eine interessante Webseite zu vielen Themen aus dem Bereich Linux und Windows. Man findet hier Tipps und Tricks zu cms php apache postfix openxchange tomcat windows linux firewall <br><br><font face='Verdana' size='2'><li><a href='cms.php?print=&aktion=thema_anzeigen&menue_id=191'>Startseite</a><font face='Verdana' size='2'><li><a href='cms.php?print=&aktion=thema_anzeigen&menue_id=63'>Knowledge base</a><font face='Verdana' size='2'><li><a href='cms.php?print=&aktion=thema_anzeigen&menue_id=57'>Kontakt</a><font face='Verdana' size='2'><li><a href='cms.php?print=&aktion=thema_anzeigen&menue_id=9'>Impressum</a></body>