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_server.c < prev    next >
C/C++ Source or Header  |  2005-04-03  |  4KB  |  103 lines

  1. /**********************************************************************************
  2. * Programma:      echo_server                                                     *
  3. *                                                                                 *
  4. * finalita':      Fa l'echo di un messaggio ricevuto                              * 
  5. *                 Quando e' lanciato, si puo' redirigere lo standard error        *
  6. *                 su un file di log di errori. Si esegue in background (&).       *
  7. *                 Ricordarsi di killarlo alla fine della prova.                   *
  8. *                                                                                 *
  9. * Sintassi:       echo_server                                                     *
  10. *                 La porta su cui e' attivato e' SERVER                           *
  11. *                 Il client e' echo_client.c, il protocollo e' TCP.               *
  12. *                                                                                 *
  13. ***********************************************************************************/
  14.  
  15. #include <errno.h>
  16. #include <stdio.h>
  17. #include <sys/types.h>
  18. #include <sys/socket.h>
  19. #include <netinet/in.h>
  20. #include <netdb.h>
  21.  
  22. #define  BACKLOG         5      /* n. max di richieste in coda */
  23. #define  MAXHOSTNAME    32      /* lunghezza massima nome host */
  24. #define  SERVER       3123      /* porta del server */
  25. char     zero[20];
  26.  
  27. main (argc, argv)
  28. int  argc;
  29. char **argv;
  30.  
  31. { int  sfd, tfd;                /* socket descr. */
  32.   int  i,err;                   
  33.   struct  sockaddr_in  sa, isa; /* indirizzi Internet */
  34.   struct  hostent  *hp;         /* risultato host name lookup */
  35.   struct  servent  *sp;         /* risultato service lookup */
  36.   char    *myname;              /* nome di questo eseguibile */
  37.   char    localhost[MAXHOSTNAME+1]; /* local host name come stringa */
  38.  
  39.   myname=argv[0];
  40.  
  41.   for (i=1; i<21; i++) {zero[i]='\0';}
  42.   /* chiede l'indirizzo del local host */
  43.   gethostname(localhost,MAXHOSTNAME);
  44.   if ((hp=gethostbyname(localhost))==NULL)  {
  45.      fprintf(stderr, "%s:  errore local host \n", myname);
  46.      exit(1);
  47.     }
  48.  
  49.   /* inserisce il socket number dentro la struttura del socket */
  50.   sa.sin_port=htons((short int) SERVER);
  51.  
  52.   /* costruisce la struttura che contiene l'indirizzo del local host */ 
  53.   bcopy((char *) hp->h_addr, (char *) &sa.sin_addr, hp->h_length);
  54.   sa.sin_family=hp->h_addrtype;
  55.  
  56.   /* richiede un descrittore di socket */
  57.   if (( sfd=socket(hp->h_addrtype, SOCK_STREAM, 0))<0 )   {
  58.      fprintf(stderr, "%s:  errore apertura socket \n", myname);
  59.      exit;
  60.   }
  61.   /* fa il bind alla service port */
  62.   if ((err=bind(sfd, (struct sockaddr *)&sa, sizeof sa))<0)   { 
  63.      fprintf (stderr, "%s:  errore %d   \n", myname, errno);
  64.      fprintf (stderr, "%s:  bind rifiutato, errore %d   porta %d\n", 
  65.                myname, err, ntohs(sa.sin_port));
  66.      exit(1);
  67.      }
  68.  
  69.   /* massimo numero di connessioni accettate */
  70.   listen(sfd, BACKLOG);
  71.  
  72.   /* loop infinita di attesa richieste */
  73.   for (;;)   {
  74.       i=sizeof isa;      
  75.       if (( tfd=accept(sfd, (struct sockaddr *)&isa, &i))<0)   {
  76.           printf("%s:  errore accept \n", myname);
  77.           exit(1);
  78.       }
  79.       echo(tfd);        /* esegue quanto richiesto */
  80.       close(tfd);
  81.       }
  82. }
  83.  
  84.  
  85. echo(sock)
  86. int sock;
  87.  
  88. {  char    buf[BUFSIZ], ret[BUFSIZ];
  89.    int     i;
  90.  
  91.    /* legge una richiesta */
  92.    if ((i=read(sock, buf, BUFSIZ))<= 0)
  93.        strcpy(ret, "non ho letto niente \n");
  94.    else {
  95.           buf[i]='\0';           /* terminatore nullo */
  96.           sprintf(ret, "ho letto %s \n", buf);
  97.         }
  98.    /* invia la risposta */
  99.    write(sock, ret, strlen(ret));
  100.    strcpy(ret, zero);
  101.    return;
  102. }
  103.