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

  1. /**********************************************************************************
  2. * Programma:      TCPtimeserver.c                                                 *
  3. *                                                                                 *
  4. * finalita':      Restituisce il valore di time ai processi che si connettono     *
  5. *                 il client e' TCPtimeclient.c, il protocollo e' TCP.             *
  6. *                                                                                 *
  7. * Sintassi:       TCPtimeserver [ port number ]                                   *
  8. *                            port  - port number da abilitare                     *
  9. *                 Il port number e' opzionale, se non passato si usa SERVER_PORT  *
  10. *                                                                                 *
  11. ***********************************************************************************/
  12.  
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <errno.h>
  17. #include <stdio.h>
  18. #include <netdb.h>
  19.  
  20.  
  21. #define SERVER_PORT        3123          
  22. #define QLEN                  5            // lunghezza coda accept
  23. #define MAXHOSTNAME         100
  24.  
  25.  
  26. #define SIZE 1024
  27. char buf[SIZE];
  28.  
  29. int main(int argc, char *argv[])
  30. {
  31.   int sockfd, client_sockfd;
  32.   int nread, port, len, i;
  33.   struct sockaddr_in serv_addr, client_addr;
  34.   time_t t;
  35.   char localhost[MAXHOSTNAME+1];
  36.   struct  hostent  *hp;         
  37.  
  38.   if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
  39.   {
  40.     perror(NULL);
  41.     exit(2);
  42.   }
  43.  
  44.   // trovo il nome DNS dell'host
  45.   gethostname(localhost,MAXHOSTNAME);
  46.  
  47.   // trovo la struttura hostent dell'host
  48.   // ATTENZIONE, non usare gethostid!!!
  49.  
  50.   if ((hp=gethostbyname(localhost))==NULL)  {
  51.      printf("server:  errore local host \n");
  52.      exit(1);
  53.     }
  54.  
  55.   // assegna l'indirizzo locale per il bind
  56.   memset((char  *)&serv_addr, 0, sizeof(serv_addr)); 
  57.   serv_addr.sin_family = AF_INET;
  58.     /* costruisce la struttura che contiene l'indirizzo del local host */ 
  59.   bcopy((char *) hp->h_addr, (char *) &serv_addr.sin_addr, hp->h_length);
  60.  
  61.   // Controlla gli argomenti passati nella command-line (port) 
  62.   if (argc > 1)  port = atoi (argv[1]);     // converte in binario 
  63.   else port = SERVER_PORT;                  // default port number
  64.  
  65.   serv_addr.sin_port = htons((u_short)port);
  66.  
  67.   // associa un indirizzo locale al socket 
  68.   if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof (serv_addr)) < 0) {
  69.     perror(NULL);
  70.     exit(1);
  71.       }
  72.  
  73.    // lunghezza coda accept 
  74.    if (listen(sockfd, QLEN) < 0) {
  75.     perror(NULL);
  76.     exit(1);
  77.       }
  78.  
  79.   len=sizeof (client_addr);  
  80.  
  81. printf("server prima di accept port %d  IP %s\n", ntohs(serv_addr.sin_port), inet_ntoa(serv_addr.sin_addr));
  82.  
  83.    for (;;) {
  84.     client_sockfd = accept(sockfd, &client_addr, &len);
  85.  
  86.   if (client_sockfd <= 0) 
  87.      {
  88.       perror (NULL);
  89.       exit(1);
  90.      }
  91.  
  92.     time(&t);
  93.     sprintf(buf, "%s", asctime(localtime(&t))); 
  94.     len = strlen(buf) + 1;
  95.     write(client_sockfd, buf, len);
  96.     close(client_sockfd);
  97.   }
  98. }
  99.  
  100.