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

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