Advertisement
hadarelv

Client

Jan 17th, 2022
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <array>
  2. #include <chrono>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <netinet/in.h>
  6. #include <netinet/ip.h>
  7. #include <netdb.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <unistd.h>
  14.  
  15. void error(const char *msg)
  16. {
  17.     perror(msg);
  18.     exit(0);
  19. }
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23.  
  24.     int sockfd, portno, n;
  25.     struct sockaddr_in serv_addr;
  26.     struct hostent *server;
  27.  
  28.     double dbuffer[1]={0.0};
  29.  
  30. // Measured RTT will be written to a csv file
  31.  
  32.     std::array<std::chrono::high_resolution_clock::time_point, 2> timer;
  33.     std::chrono::duration<double, std::milli> tdiff_ms;
  34.  
  35.     if (argc < 3) {
  36.     fprintf(stderr,"usage %s hostname port\n", argv[0]);
  37.     exit(0);
  38.     }
  39.     portno = atoi(argv[2]);
  40.     // Create socket. At this point we can directly set the DSCP value
  41.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  42.  
  43. //  int priority = 192; //110 000 - voice
  44. //  if(setsockopt(sockfd, IPPROTO_IP, IP_TOS, &priority, sizeof(priority)) < 0)
  45. //  {
  46. //      printf("Priority could not be set\n");
  47. //  }
  48.  
  49.     if (sockfd < 0)
  50.         error("ERROR opening socket");
  51.     server = gethostbyname(argv[1]);
  52.  
  53.     if (server == NULL)
  54.     {
  55.         fprintf(stderr,"ERROR, no such host\n");
  56.         exit(0);
  57.     }
  58.  
  59.     bzero((char *) &serv_addr, sizeof(serv_addr));
  60.     serv_addr.sin_family = AF_INET;
  61.     bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
  62.     serv_addr.sin_port = htons(portno);
  63.  
  64.     if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  65.         error("ERROR connecting");
  66.  
  67.     printf("Start sending messages..\n");
  68.  
  69.     std::ofstream outputFile("RTT.csv");
  70.  
  71.     do
  72.     {
  73.         dbuffer[0]++;
  74.  
  75.         timer[0] = std::chrono::high_resolution_clock::now(); // start
  76.  
  77.         printf("Sending %f to server.\n", dbuffer[0]);
  78.         n = write(sockfd, dbuffer, sizeof(dbuffer));
  79.  
  80.         if (n < 0)
  81.             error("ERROR writing to socket");
  82.  
  83.         n = read(sockfd, dbuffer, sizeof(dbuffer));
  84.         printf("Got %f from server.\n", dbuffer[0]);
  85.  
  86.         timer[1] = std::chrono::high_resolution_clock::now(); // stop
  87.         tdiff_ms = timer[1] - timer[0];
  88.         std::cout << "Time to get message back : " << tdiff_ms.count() << " ms." << std::endl;
  89.  
  90.         // Save measured RTT to file
  91.         outputFile << tdiff_ms.count() << ",";
  92.  
  93.         if (n < 0)
  94.             error("ERROR reading from socket");
  95.     }
  96.     while (dbuffer[0] < 2000);
  97.  
  98.     outputFile.close();
  99.     close(sockfd);
  100.  
  101.     return 0;
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement