Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.16 KB | None | 0 0
  1. /********************************TESTEUR**********************************************/
  2. /*************************************************************************************/
  3.  
  4. int main(int argc, char * argv[]) {
  5. // port client
  6.     unsigned short portMiroirTCP;
  7.     unsigned short portTestUDP;
  8.     // socket client
  9.     int sockTCP;
  10.     struct sockaddr_in addr_miroirTCP;
  11.     struct hostent * hostMiroir,* hostTest;
  12.  
  13.     int buf[1];
  14.     buf[0] = 10;
  15.  
  16. // socket local
  17.     if ((sockTCP = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  18.         perror("socket");
  19.         exit(EXIT_FAILURE);
  20.     }
  21.  
  22.     // adresse IP miroir
  23.     if ((hostMiroir = gethostbyname(argv[1])) == NULL) {
  24.         perror("gethostbyname");
  25.         exit(EXIT_FAILURE);
  26.     }
  27.  
  28.     // adresse IP miroir
  29.     if ((hostTest = gethostbyname("localhost")) == NULL) {
  30.         perror("gethostbyname");
  31.         exit(EXIT_FAILURE);
  32.     }
  33.  
  34.     // préparation adresse serveur
  35.     portMiroirTCP = (unsigned short) 5000;
  36.     addr_miroirTCP.sin_family = AF_INET;
  37.     addr_miroirTCP.sin_port = htons(portMiroirTCP);
  38.     bcopy(hostMiroir->h_addr, &addr_miroirTCP.sin_addr, hostMiroir->h_length);
  39.  
  40.     // demande de connexion serveur
  41.     if (connect(sockTCP, (struct sockaddr *) &addr_miroirTCP, sizeof(addr_miroirTCP)) == -1) {
  42.         perror("connect");
  43.         exit(EXIT_FAILURE);
  44.     }
  45.  
  46.     // connexion OK
  47.     printf("Connexion acceptée.\n");
  48.  
  49.     // envoi de la commande
  50.     ssize_t wr;
  51.     if ((wr = write(sockTCP, buf, sizeof(int))) == -1) {
  52.         perror("write");
  53.         exit(EXIT_FAILURE);
  54.     }
  55.  
  56.     //creation socket UDP
  57.     int sockUDP;
  58.  
  59.  
  60.     //declaration des compteurs
  61.     int nbMsg = 0;  
  62.     int nbInvers = 0;
  63.     struct sockaddr_in addr_miroirUDP,addr_testUDP;
  64.  
  65.     // Creating socket file descriptor
  66.     if ( (sockUDP = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
  67.         perror("socket creation failed");
  68.         exit(EXIT_FAILURE);
  69.     }
  70.    
  71.     memset(&addr_miroirUDP, 0, sizeof(addr_miroirUDP));
  72.     memset(&addr_testUDP, 0, sizeof(addr_testUDP));
  73.    
  74.     // Filling server information
  75.     portTestUDP = (unsigned short) 4000 ;
  76.     addr_testUDP.sin_family    = AF_INET; // IPv4
  77.     addr_testUDP.sin_addr.s_addr = htonl(INADDR_ANY);
  78.     addr_testUDP.sin_port = htons(portTestUDP);
  79.     bcopy(hostTest->h_addr, &addr_testUDP.sin_addr, hostTest->h_length);
  80.    
  81.     // Bind the socket with the server address
  82.     if ( bind(sockUDP, (const struct sockaddr *)&addr_testUDP,  
  83.             sizeof(addr_testUDP)) < 0 )
  84.     {
  85.         perror("bind failed");
  86.         exit(EXIT_FAILURE);
  87.     }  
  88.    
  89.     int n,msg,msg_av = -1;
  90.     socklen_t len_addr_miroirUDP;
  91.  
  92.     //SELECT
  93.     fd_set fd_set_read;
  94.  
  95.     /* Initialize the set of active sockets. */
  96.     FD_ZERO (&fd_set_read);
  97.     FD_SET (sockTCP, &fd_set_read);
  98.     FD_SET (sockUDP, &fd_set_read);
  99.    
  100.        
  101.     if (select (sockUDP+1, &fd_set_read, NULL, NULL, NULL) < 0)
  102.         {
  103.           perror ("select");
  104.           exit (EXIT_FAILURE);
  105.         }
  106.  
  107.     if (FD_ISSET (sockUDP, &fd_set_read)){
  108.         while(nbMsg<buf[0]){
  109.             if((n = recvfrom(sockUDP, (int *)&msg, sizeof(int),0,(struct sockaddr *) &addr_miroirUDP,(socklen_t *)&len_addr_miroirUDP)) ==-1){
  110.                 perror("recvfrom failed");
  111.                 exit(EXIT_FAILURE);
  112.             }
  113.             printf("message recu = %d\n",msg);
  114.  
  115.             nbMsg++;
  116.             if(msg_av > msg){
  117.                 nbInvers++;
  118.             }
  119.             msg_av = msg;  
  120.         }
  121.     }
  122.  
  123.     if (FD_ISSET (sockTCP, &fd_set_read)){
  124.         // réception de la réponse de CONCERT
  125.         char fin[4];
  126.         ssize_t rd;
  127.         if ((rd = read(sockTCP, fin, 4)) != 4) {
  128.             perror("read");
  129.             exit(EXIT_FAILURE);
  130.         }
  131.         printf("message FIN recu\n");
  132.  
  133.         FD_CLR(sockUDP,&fd_set_read);
  134.         FD_CLR(sockTCP,&fd_set_read);
  135.        
  136.     }
  137.  
  138.     //traitement des resultats
  139.     printf("recu %d %% de messages\n",(nbMsg/buf[0]*100));
  140.     printf("%d %% de messages invercee\n",(nbInvers/buf[0]*100));
  141.  
  142.     close(sockUDP);
  143.     close(sockTCP);
  144.     printf("les sockets ferme\n");
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement