Advertisement
Mohamed_AIT_RAMI

Untitled

Jul 16th, 2020
998
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.08 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.  
  30.     //initialise all client_socket[] to 0 so not checked
  31.     for (i = 0; i < max_clients; i++)
  32.     {
  33.         client_socket[i] = 0;
  34.     }
  35.      
  36.     //create a master socket
  37.     if( (master_socket = socket(AF_INET , SOCK_STREAM , 0)) == 0)
  38.     {
  39.         perror("socket failed");
  40.         exit(EXIT_FAILURE);
  41.     }
  42.  
  43.     //set master socket to allow multiple connections , this is just a good habit, it will work without this
  44.  
  45.     //type of socket created
  46.     address.sin_family = AF_INET;
  47.     address.sin_addr.s_addr = INADDR_ANY;
  48.     address.sin_port = htons( PORT );
  49.      
  50.     //bind the socket to localhost port 8888
  51.     if (bind(master_socket, (struct sockaddr *)&address, sizeof(address))<0)
  52.     {
  53.         perror("bind failed");
  54.         exit(EXIT_FAILURE);
  55.     }
  56.      
  57.     //try to specify maximum of 30 pending connections for the master socket
  58.     if (listen(master_socket, 30) < 0)
  59.     {
  60.         perror("listen");
  61.         exit(EXIT_FAILURE);
  62.     }
  63.      
  64.     //accept the incoming connection
  65.     addrlen = sizeof(address);
  66.      
  67.     while(TRUE)
  68.     {
  69.         //clear the socket set
  70.       //  FD_ZERO(&readfds);
  71.  
  72.         //add master socket to set
  73.         FD_SET(master_socket, &readfds);
  74.         max_sd = master_socket;
  75.          
  76.         //add child sockets to set
  77.         for ( i = 0 ; i < max_clients ; i++)
  78.         {
  79.             //socket descriptor
  80.             sd = client_socket[i];
  81.              
  82.             //if valid socket descriptor then add to read list
  83.             if(sd > 0)
  84.                 FD_SET( sd , &readfds);
  85.              
  86.             //highest file descriptor number, need it for the select function
  87.             if(sd > max_sd)
  88.                 max_sd = sd;
  89.         }
  90.  
  91.         //wait for an activity on one of the sockets , timeout is NULL , so wait indefinitely
  92.         activity = select( max_sd + 1 , &readfds , NULL , NULL , NULL);
  93.    
  94.         //If something happened on the master socket , then its an incoming connection
  95.         if (FD_ISSET(master_socket, &readfds))
  96.         {
  97.             if ((new_socket = accept(master_socket, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
  98.             {
  99.                 perror("accept");
  100.                 exit(EXIT_FAILURE);
  101.             }
  102.        
  103.             //send new connection greeting message
  104.             if( send(new_socket, message, strlen(message), 0) != strlen(message) )
  105.             {
  106.                 perror("send");
  107.             }
  108.              
  109.             //add new socket to array of sockets
  110.             for (i = 0; i < max_clients; i++)
  111.             {
  112.                 //if position is empty
  113.                 if( client_socket[i] == 0 )
  114.                 {
  115.                     client_socket[i] = new_socket;
  116.                     break;
  117.                 }
  118.             }
  119.         }
  120.          
  121.         //else its some IO operation on some other socket :)
  122.         for (i = 0; i < max_clients; i++)
  123.         {
  124.             sd = client_socket[i];
  125.              
  126.             if (FD_ISSET( sd , &readfds))
  127.             {   // COMMENCE L'INTERACTION AVEC LE CLIENT
  128.                 //Check if it was for closing , and also read the incoming message
  129.                 if ((valread = read( sd , buffer, 1024)) == 0)
  130.                 {
  131.                     //Somebody disconnected , get his details and print
  132.                     getpeername(sd , (struct sockaddr*)&address , (socklen_t*)&addrlen);
  133.                     close( sd );
  134.                     client_socket[i] = 0;
  135.                 }
  136.                  
  137.                 //Echo back the message that came in
  138.                 else
  139.                 {
  140.                     //c'est la où commence l'intéraction avec le client ,mn hna changer
  141.                     /*
  142.                         par exemple : écrire un serveur multi-client dont le client envoie au serveur
  143.                         un nombre et le serveur lui retourne le double de ce nombre
  144.                     */
  145.                    
  146.                    
  147.                     read(  new_socket , buffer, 1024);
  148.                     printf("write the message you want to send to your clients:\n");
  149.                     scanf("%s", &reponse);
  150.                     strcpy(reponse,"accessible");                
  151.  
  152.                   send( new_socket , reponse , strlen(reponse) , 0 );
  153.                
  154.                 }
  155.             }
  156.         }
  157.     }
  158.      
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement