Advertisement
Guest User

server

a guest
Jan 8th, 2012
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <arpa/inet.h>
  7. #include <netinet/in.h>
  8. #include <unistd.h>
  9. #include <signal.h>
  10. #include <wait.h>
  11. #include <ctype.h>
  12.  
  13. void ripulisci (char* stringa);
  14. void minuscola (char* orig, char* dest);
  15. void maiuscola (char* orig, char* dest);
  16. void handler(int);
  17.  
  18. int list;
  19. int sock;
  20.  
  21. int main () {
  22.     int status;
  23.  
  24.     //creo un socket da mettere in ascolto
  25.     list = socket (AF_INET, SOCK_STREAM, 0);
  26.     if (list < 0) {
  27.         perror("Error!");
  28.         exit(1);
  29.     }
  30.  
  31.     //preparo la struct sockaddr_in
  32.     struct sockaddr_in address;
  33.     address.sin_family = AF_INET;
  34.     address.sin_addr.s_addr = INADDR_ANY;
  35.     address.sin_port = htons(2770);
  36.  
  37.     //effettuo il bind
  38.     status = bind(list, (struct sockaddr*) &address, sizeof(address));
  39.     if (status < 0) {
  40.         perror("Error!");
  41.         close(list);
  42.         exit(1);
  43.     }
  44.  
  45.     //mi metto in ascolto
  46.     listen(list, 1);
  47.     printf("Attendo le connessioni...\n");
  48.  
  49.     //preparo le variabili necessarie per gestire i figli
  50.     int esci = 1;
  51.     pid_t server[5];
  52.     char comando[15];
  53.     char lettura[512];
  54.     char scrittura[512];
  55.     struct sockaddr_in client;
  56.     int client_size = sizeof(client);
  57.     fd_set fd;
  58.     FD_ZERO(&fd);
  59.     FD_SET(list, &fd);
  60.     struct timeval timer;
  61.  
  62.     //genero i quattro figli/server e mi salvo i pid in un array
  63.     int index;
  64.     for (index=0 ; index<5 ; index++) {
  65.         server[index] = fork();
  66.         if (server[index] == 0)
  67.             break;
  68.     }
  69.  
  70.     //verifico quale processo sono
  71.     if (index == 5) {
  72.         pause(); //aspetto un segnale da uno dei figli
  73.     }
  74.  
  75.     //sono un figlio/server
  76.     else {
  77.         while(esci) {
  78.             timer.tv_sec = 1;
  79.             timer.tv_usec = 0;
  80.             if (select(list+1, &fd, NULL, NULL, &timer) <= 0) {
  81.                 printf("Nessun client da servire\n");
  82.                 continue;
  83.             }
  84.             if (!FD_ISSET(list, &fd))
  85.                 continue;
  86.             printf("C'è un client\n");
  87.             sock = accept(list, (struct sockaddr*) &client, (socklen_t*) &client_size);
  88.             if (sock < 0)
  89.                 break;
  90.             printf("Connesso con il client\n");
  91.             recv(sock, comando, 15, 0);
  92.             recv(sock, lettura, 512, 0);
  93.             if (comando[0] == 'F' && comando[1] == 'I' && comando[2] == 'N' && comando[3] == 'E' && comando[4] == '\0')
  94.                 kill(getppid(), SIGALRM); //al termine dell'esecuzione uscirò
  95.             ripulisci(lettura);
  96.  
  97.             //il comando è tuttomaiuscole
  98.             if (strcmp("tuttomaiuscolo", comando) == 0) {
  99.                 maiuscola(lettura, scrittura);
  100.                 send(sock, scrittura, 512, 0);
  101.             }
  102.  
  103.             //il comando è tuttominuscole
  104.             else if (strcmp("tuttominuscolo", comando) == 0) {
  105.                 minuscola(lettura, scrittura);
  106.                 send(sock, scrittura, 512, 0);
  107.             }
  108.  
  109.             //c'è stato un errore
  110.             else {
  111.                 printf ("Error! Command not found\n");
  112.                 strcpy (scrittura, "error");
  113.                 send (sock, scrittura, 512, 0);
  114.             }
  115.             printf("Devo terminare!");
  116.             close(sock);
  117.             exit(0);
  118.         }
  119.     }
  120.  
  121.     //termino tutto
  122.     for(index=0 ; index<5 ; index++) {
  123.         waitpid((int)server[index], NULL, 0);
  124.     }
  125.     exit(0);
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. void handler(int sig) {
  136.     signal(sig, SIG_IGN);
  137.     printf("Il server sta terminando in seguito ad una richiesta\n");
  138.     close (list);
  139.     close (sock);
  140.     exit(0);
  141. }
  142.  
  143. void ripulisci (char* stringa) {
  144.     int index = 0;
  145.     int app;
  146.     while (stringa[index] != '\0' && index<=511) {
  147.         if (isalpha(stringa[index])!=1) {
  148.             app = index;
  149.             do {
  150.                 stringa[app] = stringa[app+1];
  151.                 app++;
  152.             } while (stringa[app] != '\0');
  153.             stringa[app] = '\0';
  154.             index--;
  155.         }
  156.         index++;
  157.     }
  158.     return;
  159. }
  160.  
  161. void minuscola (char* orig, char* dest) {
  162.     int index = 0;
  163.     do {
  164.         if (orig[index] < 91)
  165.             dest[index] = toupper(orig[index]);
  166.         else
  167.             dest[index] = orig[index];
  168.         index++;
  169.     } while (orig[index] != '\0');
  170.     dest[index] = '\0';
  171.     return;
  172. }
  173.  
  174. void maiuscola (char* orig, char* dest) {
  175.     int index = 0;
  176.     do {
  177.         if (orig[index] > 91)
  178.             dest[index] = tolower(orig[index]);
  179.         else
  180.             dest[index] = orig[index];
  181.         index++;
  182.     } while (orig[index] != '\0');
  183.     dest[index] = '\0';
  184.     return;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement