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 / echo_client.c < prev    next >
C/C++ Source or Header  |  2005-04-03  |  3KB  |  85 lines

  1. /**********************************************************************************
  2. * Programma:      echo_client                                                     *
  3. *                                                                                 *
  4. * finalita':      si collega con un server (echo_server)                          * 
  5. *                 Invia al server una stringa letta sullo standard input          *
  6. *                 e la riceve come eco                                            *
  7. *                                                                                 *
  8. * Sintassi:       echo_client [ host ]                                            *
  9. *                            host  - nome dell'host su cui risiede il server      *
  10. *                                                                                 *
  11. ***********************************************************************************/
  12.  
  13. #include <stdio.h>
  14. #include <sys/types.h>
  15. #include <sys/socket.h>
  16. #include <netinet/in.h>
  17. #include <netdb.h>
  18. #define  SERVER    3123
  19.  
  20. char  stringa[20];            /* stringa da fare echo */
  21.  
  22. main (int argc, char **argv)
  23. { int  sockfd;            /* socket descr. */
  24.   int  len;            /* n.ro byte dati ricevuti */
  25.   struct  sockaddr_in  sa;    /* indirizzo Internet */
  26.   struct  hostent  *hp;        /* risultato host name lookup */
  27.   struct  servent  *sp;        /* risultato service lookup */
  28.   char  buf[BUFSIZ];        /* informazioni da whois */
  29.   char  *myname;        /* nome di questo eseguibile */
  30.   char  *host;            /* nome dell'host remoto */
  31.  
  32.   myname=argv[0];
  33.   if (argc != 2) {
  34.      printf("Usage: %s  host  \n", myname);
  35.      exit(1);
  36.      }
  37.   host=argv[1];
  38.  
  39.   if (( hp=gethostbyname(host) )==NULL)  {
  40.      printf("%s:  %s:  no such host \n", myname, host);
  41.      exit(1);
  42.     }
  43.  
  44.   /* costruisce la struttura che contiene l'indirizzo dell'host 
  45.      da contattare, e il tipo di indirizzamento usato */
  46.  
  47.   bcopy((char *) hp->h_addr, (char *) &sa.sin_addr, hp->h_length);
  48.   sa.sin_family=hp->h_addrtype;
  49.  
  50.   /* inserisce il socket number dentro la struttura del socket */
  51.  
  52.   sa.sin_port=htons((short int) SERVER);
  53.  
  54.   /* richiede un descrittore di socket */
  55.  
  56.   if (( sockfd=socket(hp->h_addrtype, SOCK_STREAM, 0))<0 )   {
  57.       printf("%s:  errore apertura socket \n", myname);
  58.       exit(1);
  59.   }
  60.  
  61.   /* mi connetto con il server remoto */
  62.  
  63.   if (connect(sockfd, (struct sockaddr *) &sa, sizeof sa)<0)   {
  64.      printf ("%s:  connessione rifiutata \n", myname);
  65.      exit(1);
  66.   }
  67.  
  68.   printf("%s: stringa da inviare > ", myname);
  69.   scanf("%s",stringa);
  70.  
  71.   if (write(sockfd, stringa, strlen(stringa)) != strlen(stringa))   {
  72.      printf("%s:  write error \n", myname);
  73.      exit(1);
  74.   }
  75.  
  76.   /* ricevo la replica */
  77.  
  78.   while ((len=read(sockfd, buf, BUFSIZ))>0) 
  79.          write(0, buf, len);
  80.   close(sockfd);
  81.   exit(0);
  82. }
  83.  
  84.  
  85.