SHARE
TWEET

Untitled

a guest Mar 25th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /******************************************MIROIR*****************************************/
  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_RDV,sockTCP_Service,sockUDP;
  10.     struct sockaddr_in addr_TestTCP,addr_TestUDP;
  11.     struct sockaddr_in addr_MiroirTCP;
  12.     struct hostent * hostMiroir,* hostTest;
  13.     // Declaration des buffers
  14.     int buf[1];
  15.    
  16.     /* Creation de la socketTCP*/
  17.     if ((sockTCP_RDV = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  18.         perror("socket");
  19.         exit(EXIT_FAILURE);
  20.     }
  21.    
  22.      // adresse IP Test
  23.     if ((hostTest = gethostbyname(argv[1])) == NULL) {
  24.         perror("gethostbyname");
  25.         exit(EXIT_FAILURE);
  26.     }
  27.      // adresse IP miroir
  28.     if ((hostMiroir = gethostbyname("localhost")) == NULL) {
  29.         perror("gethostbyname");
  30.         exit(EXIT_FAILURE);
  31.     }
  32.     /*Preparation de l'adresse local*/
  33.     portMiroirTCP = (unsigned short) 5000;
  34.     addr_MiroirTCP.sin_family = AF_INET;
  35.     addr_MiroirTCP.sin_port = htons(portMiroirTCP);
  36.     bcopy(hostMiroir->h_addr, &addr_MiroirTCP.sin_addr, hostMiroir->h_length);
  37.  
  38.     /*Attachement de la socket */
  39.     int lg_addr_MiroirTCP = sizeof(addr_MiroirTCP);
  40.     if((bind(sockTCP_RDV,(struct sockaddr *)&addr_MiroirTCP,lg_addr_MiroirTCP))== -1){
  41.         perror("bind");
  42.             exit(-1);
  43.     }
  44.     /* OUVERTURE DU SERVICE*/
  45.     if(listen(sockTCP_RDV,10)== -1){
  46.         perror("listen");
  47.         exit(-1);
  48.     }
  49.     /* Attente du client */
  50.     socklen_t lg_addr_TestTCP=sizeof(addr_MiroirTCP);
  51.     sockTCP_Service=accept(sockTCP_RDV,(struct sockaddr *)&addr_TestTCP,(socklen_t *)&lg_addr_TestTCP);
  52.     if(sockTCP_Service==-1 && errno==EINTR){
  53.      
  54.     }
  55.     if(sockTCP_Service == -1){
  56.         perror("accept");
  57.         exit(-1);
  58.     }
  59.     printf("Testeur connecte\n");
  60.     fflush(stdout);
  61.  
  62.     if(read(sockTCP_Service, buf,sizeof(int))<0){
  63.         perror("read");
  64.         exit(-1);
  65.     }
  66.     printf("N = %d\n",buf[0]);
  67.     /* Creation de la socketUDP*/
  68.  
  69.     if ( (sockUDP = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
  70.         perror("socket creation failed");
  71.         exit(EXIT_FAILURE);
  72.     }
  73.      /*Preparation de l'adresse local*/
  74.     portTestUDP = (unsigned short) 4000;
  75.     addr_TestUDP.sin_family = AF_INET;
  76.     addr_TestUDP.sin_port = htons(portTestUDP);
  77.     bcopy(hostTest->h_addr, &addr_TestUDP.sin_addr, hostTest->h_length);    
  78.  
  79.    
  80.     int lg_addr_TestUDP= sizeof(addr_TestUDP);
  81.     int envoye,i;
  82.  
  83.     for(i=0;i<buf[0];i++){
  84.         if((envoye=sendto(sockUDP,&i,sizeof(int),0,(const struct sockaddr *)&addr_TestUDP,lg_addr_TestUDP)) != sizeof(int)){
  85.             perror("sendto");
  86.             exit(-1);
  87.         }
  88.         printf("message %d envoye\n",i);
  89.     }
  90.  
  91.     char fin[4]="FIN";
  92.      ssize_t wr;
  93.     if ((wr = write(sockTCP_Service, fin, strlen(fin)+1)) !=  strlen(fin)+1) {
  94.         perror("write");
  95.         exit(EXIT_FAILURE);
  96.     }
  97.     printf("FIN envoye\n");
  98.  
  99.     close(sockTCP_RDV);
  100.     close(sockTCP_Service);
  101.     close(sockUDP);
  102.     return 0;
  103. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top