Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <array>
- #include <chrono>
- #include <fstream>
- #include <iostream>
- #include <netinet/in.h>
- #include <netinet/ip.h>
- #include <netdb.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <unistd.h>
- void error(const char *msg)
- {
- perror(msg);
- exit(0);
- }
- int main(int argc, char *argv[])
- {
- int sockfd, portno, n;
- struct sockaddr_in serv_addr;
- struct hostent *server;
- double dbuffer[1]={0.0};
- // Measured RTT will be written to a csv file
- std::array<std::chrono::high_resolution_clock::time_point, 2> timer;
- std::chrono::duration<double, std::milli> tdiff_ms;
- if (argc < 3) {
- fprintf(stderr,"usage %s hostname port\n", argv[0]);
- exit(0);
- }
- portno = atoi(argv[2]);
- // Create socket. At this point we can directly set the DSCP value
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- // int priority = 192; //110 000 - voice
- // if(setsockopt(sockfd, IPPROTO_IP, IP_TOS, &priority, sizeof(priority)) < 0)
- // {
- // printf("Priority could not be set\n");
- // }
- if (sockfd < 0)
- error("ERROR opening socket");
- server = gethostbyname(argv[1]);
- if (server == NULL)
- {
- fprintf(stderr,"ERROR, no such host\n");
- exit(0);
- }
- bzero((char *) &serv_addr, sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
- serv_addr.sin_port = htons(portno);
- if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
- error("ERROR connecting");
- printf("Start sending messages..\n");
- std::ofstream outputFile("RTT.csv");
- do
- {
- dbuffer[0]++;
- timer[0] = std::chrono::high_resolution_clock::now(); // start
- printf("Sending %f to server.\n", dbuffer[0]);
- n = write(sockfd, dbuffer, sizeof(dbuffer));
- if (n < 0)
- error("ERROR writing to socket");
- n = read(sockfd, dbuffer, sizeof(dbuffer));
- printf("Got %f from server.\n", dbuffer[0]);
- timer[1] = std::chrono::high_resolution_clock::now(); // stop
- tdiff_ms = timer[1] - timer[0];
- std::cout << "Time to get message back : " << tdiff_ms.count() << " ms." << std::endl;
- // Save measured RTT to file
- outputFile << tdiff_ms.count() << ",";
- if (n < 0)
- error("ERROR reading from socket");
- }
- while (dbuffer[0] < 2000);
- outputFile.close();
- close(sockfd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement