Advertisement
Guest User

SERVER.c

a guest
Feb 8th, 2017
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.04 KB | None | 0 0
  1. //SERVER
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <arpa/inet.h>
  10.  
  11. typedef struct utente{
  12.     char ID[10];
  13.     int password;
  14.     int stato;
  15.     int vinte;
  16.     int perse;
  17.     }t_utente;
  18.  
  19. t_utente temp;
  20. int socket_list, connfd, a, n, socket_conn;
  21. struct sockaddr_in servaddr,client;
  22. char buffer[4096];
  23. char recvline[1025];
  24. char* user;
  25. int pass;
  26. pid_t pid;
  27. int one = 1;
  28. int logging = 1;
  29. int controllo = -1;
  30. int scelta = -1;
  31. int uscita = 0;
  32.  
  33. void carica(t_utente array_utenti[5]);
  34. int log_in_user(t_utente array_utenti[5]);
  35. int log_in_pass(t_utente array_utenti[5]);
  36. void stampa(t_utente array_utenti[5], int );
  37.  
  38. int main(int argc, char **argv){
  39.  
  40.     t_utente array_utenti[5];
  41.     int scelta,i,j;
  42.     carica(array_utenti);
  43.     if((socket_list = socket(AF_INET, SOCK_STREAM,0))<0){
  44.         perror("socket");
  45.         exit(1);
  46.     }
  47.     servaddr.sin_family = AF_INET;
  48.     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  49.     servaddr.sin_port = htons(3100);
  50.     if(setsockopt(socket_list,SOL_SOCKET,SO_REUSEADDR,&one,sizeof(one))<0){
  51.         printf("errore");
  52.         exit(1);
  53.     } printf("\nCONNESSIONE 1 [OK]");
  54.     if(bind(socket_list,(struct sockaddr *) &servaddr,sizeof(servaddr)) < 0){
  55.         perror("bind");
  56.         exit(1);
  57.     } printf("\nCONNESSIONE 2 [OK]");
  58.     if(listen(socket_list, 1024) < 0){
  59.         perror("listen");
  60.         exit(1);
  61.     } printf("\nCONNESSIONE 3 [OK]");
  62.     while(1){
  63.         if((socket_conn = accept(socket_list,(struct sockaddr *) NULL, NULL)) < 0){
  64.             perror("accept");
  65.             exit(1);
  66.         } printf("\nCONNESSIONE STABILITA\n");
  67.         if((pid = fork()) < 0){
  68.             perror("\nfork error");
  69.             exit(-1);
  70.         }
  71.         if(pid == 0){
  72.             close(socket_list);
  73.  
  74.             if(logging){
  75.                 inet_ntop(AF_INET,&client.sin_addr,buffer,sizeof(buffer));
  76.                 printf("\nLOGGING HOST: %s \nPORT:%d\n",buffer,ntohs(client.sin_port));
  77.             }
  78.             while(uscita != 1){
  79.                 i = log_in_user(array_utenti);
  80.                 printf("\n\nPUNT user[%d]",i);
  81.                 j = log_in_pass(array_utenti);
  82.                 printf("\n\nPUNT pass[%d]",j);
  83.                 if(i == j){
  84.                     array_utenti[j].stato = 1;
  85.                                         stampa(array_utenti,j);
  86.                                         controllo = 1;
  87.                                         write(socket_conn,&controllo,sizeof(controllo));
  88.                                         printf("\nUTENTE ACCETTATO\n");
  89.                                         uscita = 1;
  90.                 }
  91.                 else{
  92.                     printf("\nUTENTE SCONOSCIUTO\n");
  93.                     exit(1);
  94.                 }
  95.             }
  96.             while(1){
  97.                 printf("\nSCELTA\n");
  98.                 n = read(socket_conn,recvline,1024);
  99.                 recvline[n] = 0;
  100.                                 if(fputs(recvline,stdout) == EOF){
  101.                                     fprintf(stderr,"fputs error\n");
  102.                                                 exit(1);
  103.                                 }
  104.                                 scelta = atoi(recvline);
  105.                                 switch(scelta){
  106.                                     case 0: break;
  107.                     case 1: break;
  108.                                         case 2: for(i=0;i<5;i++){
  109.                                 printf("\n[%d] = %s\t%d\n",i,array_utenti[i].ID,array_utenti[i].stato);
  110.                             if(array_utenti[i].stato == 1)
  111.                                     stampa(array_utenti,i);
  112.                             else
  113.                                     continue;
  114.                            }
  115.                                                 break;
  116.                                         case 3: break;
  117.                     case 4: exit(1);
  118.                     }
  119.             }
  120.         }//pid
  121.     }//primo while
  122. }//main
  123.  
  124. void carica(t_utente array_utenti[5]){
  125.     int i;
  126.     strcpy(array_utenti[0].ID,"Antony1994");
  127.     array_utenti[0].password = 1000;
  128.  
  129.     strcpy(array_utenti[1].ID,"Yotobi7000");
  130.     array_utenti[1].password = 1010;
  131.  
  132.     strcpy(array_utenti[2].ID,"Napoli1926");
  133.     array_utenti[2].password = 1020;
  134.  
  135.     strcpy(array_utenti[3].ID,"Hamsik0017");
  136.     array_utenti[3].password = 1030;
  137.  
  138.     strcpy(array_utenti[4].ID,"Maradona10");
  139.     array_utenti[4].password = 1040;
  140.  
  141.     for(i=0;i<5;i++){
  142.         array_utenti[i].vinte = 0;
  143.         array_utenti[i].perse = 0;
  144.         array_utenti[i].stato = 0;
  145.     }
  146. }
  147.  
  148. int log_in_user(t_utente array_utenti[5]){
  149.     int i=0,n,pass;
  150.     printf("Inserire ID: ");
  151.     n = read(socket_conn,recvline,10);
  152.     recvline[n] = 0;
  153.     if(fputs(recvline,stdout) == EOF){
  154.         fprintf(stderr,"fputs error\n");
  155.         exit(1);
  156.     }
  157.     user = recvline;
  158.     printf("\nID Inserito: %s",user);
  159.     printf("\n(%lu) - lunghezza stringa passata",strlen(user));
  160.     for(i=0;i<5;i++){
  161.         printf("\n(%lu) - lunghezza stringa [%d]",strlen(array_utenti[i].ID),i);
  162.     }
  163.     for(i=0;i<5;i++){
  164.         if(strcmp(array_utenti[i].ID,user) == 0){
  165.             return i;
  166.         }
  167.     }
  168.     return -1;
  169. }
  170.  
  171. int log_in_pass(t_utente array_utenti[5]){
  172.     int j=0,n,pass;
  173.     printf("\n\nInserire PASSWORD: ");
  174.     n = read(socket_conn,recvline,1024);
  175.     recvline[n] = 0;
  176.     if(fputs(recvline,stdout) == EOF){
  177.         fprintf(stderr,"fputs error\n");
  178.         exit(1);
  179.     }
  180.     pass = atoi(recvline);
  181.     printf("\nPASSWORD Inserita: %d",pass);
  182.  
  183.     for(j=0;j<5;j++){
  184.         if(array_utenti[j].password == pass)
  185.             return j;
  186.     }
  187.     return -1;
  188. }
  189.  
  190. void stampa(t_utente array_utenti[], int y){
  191.     temp = array_utenti[y];
  192.     write(socket_conn,&temp,sizeof(temp));
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement