Advertisement
Mohamed_AIT_RAMI

Untitled

Jul 16th, 2020
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>   //strlen
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <unistd.h>   //close
  6. #include <arpa/inet.h>    //close
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #include <sys/time.h> //FD_SET, FD_ISSET, FD_ZERO macros
  11.  
  12. #define TRUE   1
  13. #define FALSE  0
  14. #define PORT 8888
  15.  
  16.  
  17. int main(int argc , char *argv[])
  18. {
  19.     int opt = 1;
  20.     int master_socket , addrlen , new_socket , client_socket[30] , max_clients = 30 , activity, i , valread , sd;
  21.     int max_sd;
  22.     struct sockaddr_in address;
  23.      
  24.     char buffer[1025],reponse[1025];  //data buffer of 1K
  25.      
  26.     //set of socket descriptors
  27.     fd_set readfds;
  28.      
  29.     //a message
  30.     char *message = "ECHO Daemon v1.0 \r\n";
  31.  
  32.     //initialise all client_socket[] to 0 so not checked
  33.     for (i = 0; i < max_clients; i++)
  34.     {
  35.         client_socket[i] = 0;
  36.     }
  37.      
  38.     //create a master socket
  39.     if( (master_socket = socket(AF_INET , SOCK_STREAM , 0)) == 0)
  40.     {
  41.         perror("socket failed");
  42.         exit(EXIT_FAILURE);
  43.     }
  44.  
  45.     //set master socket to allow multiple connections , this is just a good habit, it will work without this
  46.  
  47.     //type of socket created
  48.     address.sin_family = AF_INET;
  49.     address.sin_addr.s_addr = INADDR_ANY;
  50.     address.sin_port = htons( PORT );
  51.      
  52.     //bind the socket to localhost port 8888
  53.     if (bind(master_socket, (struct sockaddr *)&address, sizeof(address))<0)
  54.     {
  55.         perror("bind failed");
  56.         exit(EXIT_FAILURE);
  57.     }
  58.     printf("Listener on port %d \n", PORT);
  59.      
  60.     //try to specify maximum of 30 pending connections for the master socket
  61.     if (listen(master_socket, 30) < 0)
  62.     {
  63.         perror("listen");
  64.         exit(EXIT_FAILURE);
  65.     }
  66.      
  67.     //accept the incoming connection
  68.     addrlen = sizeof(address);
  69.     puts("Waiting for connections ...");
  70.      
  71.     while(TRUE)
  72.     {
  73.         //clear the socket set
  74.       //  FD_ZERO(&readfds);
  75.  
  76.         //add master socket to set
  77.         FD_SET(master_socket, &readfds);
  78.         max_sd = master_socket;
  79.          
  80.         //add child sockets to set
  81.         for ( i = 0 ; i < max_clients ; i++)
  82.         {
  83.             //socket descriptor
  84.             sd = client_socket[i];
  85.              
  86.             //if valid socket descriptor then add to read list
  87.             if(sd > 0)
  88.                 FD_SET( sd , &readfds);
  89.              
  90.             //highest file descriptor number, need it for the select function
  91.             if(sd > max_sd)
  92.                 max_sd = sd;
  93.         }
  94.  
  95.         //wait for an activity on one of the sockets , timeout is NULL , so wait indefinitely
  96.         activity = select( max_sd + 1 , &readfds , NULL , NULL , NULL);
  97.    
  98.         //If something happened on the master socket , then its an incoming connection
  99.         if (FD_ISSET(master_socket, &readfds))
  100.         {
  101.             if ((new_socket = accept(master_socket, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
  102.             {
  103.                 perror("accept");
  104.                 exit(EXIT_FAILURE);
  105.             }
  106.          
  107.             //inform user of socket number - used in send and receive commands
  108.             printf("New connection , socket fd is %d , ip is : %s , port : %d \n" , new_socket , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
  109.        
  110.             //send new connection greeting message
  111.             if( send(new_socket, message, strlen(message), 0) != strlen(message) )
  112.             {
  113.                 perror("send");
  114.             }
  115.              
  116.             puts("Welcome message sent successfully");
  117.              
  118.             //add new socket to array of sockets
  119.             for (i = 0; i < max_clients; i++)
  120.             {
  121.                 //if position is empty
  122.                 if( client_socket[i] == 0 )
  123.                 {
  124.                     client_socket[i] = new_socket;
  125.                     printf("Adding to list of sockets as %d\n" , i);
  126.                      
  127.                     break;
  128.                 }
  129.             }
  130.         }
  131.          
  132.         //else its some IO operation on some other socket :)
  133.         for (i = 0; i < max_clients; i++)
  134.         {
  135.             sd = client_socket[i];
  136.              
  137.             if (FD_ISSET( sd , &readfds))
  138.             {   // COMMENCE L'INTERACTION AVEC LE CLIENT
  139.                 //Check if it was for closing , and also read the incoming message
  140.                 if ((valread = read( sd , buffer, 1024)) == 0)
  141.                 {
  142.                     //Somebody disconnected , get his details and print
  143.                     getpeername(sd , (struct sockaddr*)&address , (socklen_t*)&addrlen);
  144.                     printf("Host disconnected , ip %s , port %d \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
  145.                      
  146.                     //Close the socket and mark as 0 in list for reuse
  147.                     close( sd );
  148.                     client_socket[i] = 0;
  149.                 }
  150.                  
  151.                 //Echo back the message that came in
  152.                 else
  153.                 {
  154.                     //c'est la où commence l'intéraction avec le client ,mn hna changer
  155.                     /*
  156.                         par exemple : écrire un serveur multi-client dont le client envoie au serveur
  157.                         un nombre et le serveur lui retourne le double de ce nombre
  158.                     */
  159.                    
  160.                   read(  new_socket , buffer, 1024);
  161.                     if(verifier(buffer)==1)
  162.                   {
  163.                     strcpy(reponse,"accessible");
  164.                   }
  165.                   else
  166.                   {
  167.                      strcpy(reponse,"n'est pas accessible");
  168.                  
  169.                   }
  170.  
  171.                   send( new_socket , reponse , strlen(reponse) , 0 );
  172.                
  173.                 }
  174.             }
  175.         }
  176.     }
  177.      
  178.     return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement