home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / GianuzziV / SO1 / cerca_server.c < prev    next >
C/C++ Source or Header  |  2005-03-28  |  2KB  |  63 lines

  1. /* cerca_server.c
  2.    sintassi:     cerca  host  server
  3.    scrive l'indirizzo in dotted form dell'host indicato e la
  4.    well known port del server indicato.
  5.    esempio       cerca olivia ftp
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #include <netdb.h>
  13. #include <errno.h>
  14.  
  15. main (argc, argv)
  16. int  argc;
  17. char **argv;
  18.  
  19. { int  sockfd;                  /* socket descr. */
  20.   int  len;                     /* n.ro byte dati ricevuti */
  21.   struct  sockaddr_in  sa;      /* indirizzo Internet */
  22.   struct  hostent  *hp;         /* risultato host name lookup */
  23.   struct  servent  *sp;         /* risultato service lookup */
  24.   char  buf[BUFSIZ];            /* informazioni da whois */
  25.   char  *myname;                /* nome di questo eseguibile */
  26.   char  *host;                  /* nome dell'host remoto */
  27.   char  *server;                /* nome del server remoto */
  28.  
  29.   myname=argv[0];
  30.   if (argc != 3) {
  31.      printf("Usage: %s  host  server \n", myname);
  32.      exit(1);
  33.      }
  34.   host=argv[1];
  35.   server=argv[2];
  36.  
  37.   if (( hp=gethostbyname(host) )==NULL)  {
  38.      printf("%s:  %s:  no such host \n", myname, host);
  39.      exit(1);
  40.     }
  41.  
  42.   printf("%s:  nome host:%s   indirizzo: %s \n", myname, hp->h_name,
  43.            inet_ntoa(ntohl(hp->h_addr))); 
  44.  
  45.   /* costruisce la struttura che contiene l'indirizzo dell'host
  46.      da contattare, e il tipo di indirizzamento usato */
  47.  
  48.   bcopy((char *) hp->h_addr, (char *) &sa.sin_addr, hp->h_length);
  49.   sa.sin_family=hp->h_addrtype;
  50.  
  51.   /* chiede qual'e' il well known socket number per il servizio server
  52.      (localmente) */
  53.   if (( sp=getservbyname(server,"tcp"))==NULL)  {
  54.      printf ("%s:  %s:  no such server on this host \n", myname, host);
  55.      exit(1);
  56.   }
  57.  
  58.   /* inserisce il socket number dentro la struttura del socket */
  59.  
  60.   sa.sin_port=sp->s_port;
  61.   printf("%s:  server %s sulla porta numero: %d\n", myname, server, ntohs(sa.sin_port));
  62. }
  63.