Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <netdb.h>
  8. #include <arpa/inet.h>
  9. #include <string.h>
  10. #include <time.h>
  11.  
  12. #define MAXLINE 1024
  13. #define SERV_PORT 10010
  14. #define PACKET_SIZE 1038
  15.  
  16. /* Global Variables */
  17. char string[MAXLINE], packet[PACKET_SIZE];
  18. int sequenceNumber;
  19. short totalMessageLength;
  20. long timestamp;
  21.  
  22. void createPacket() {
  23. sequenceNumber = htonl(0);
  24. totalMessageLength = htons(14 + strlen(string));
  25. memcpy(packet, &totalMessageLength, sizeof(short));
  26. memcpy(packet + sizeof(short), &sequenceNumber, sizeof(int));
  27. memcpy(packet + sizeof(short) + sizeof(int), &timestamp, sizeof(long));
  28. memcpy(packet + sizeof(short) + sizeof(int) + sizeof(long), &string, strlen(string));
  29. }
  30.  
  31. void deconstructPacket() {
  32. memcpy(&totalMessageLength, packet, sizeof(short));
  33. totalMessageLength = ntohs(totalMessageLength);
  34. memcpy(&sequenceNumber, packet + sizeof(short), sizeof(int));
  35. sequenceNumber = ntohl(sequenceNumber);
  36. memcpy(&timestamp, packet + sizeof(short) + sizeof(int), sizeof(long));
  37. timestamp = be64toh(timestamp);
  38. memcpy(&string, packet + sizeof(short) + sizeof(int) + sizeof(long), MAXLINE);
  39. }
  40.  
  41. int main(int argc, char **argv) {
  42. int dataSocket, n;
  43. struct hostent* serverHost;
  44. socklen_t serverLength;
  45. struct timeval sendTime, recvTime;
  46. long recvTimeMS, RTT;
  47. struct sockaddr_in serverSocketAddress;
  48.  
  49. /* Argument check */
  50. if (argc != 2) {
  51. perror("Usage: ./clientName 'Host name of the server'");
  52. exit(1);
  53. }
  54.  
  55. /* Extract IP from Hostname */
  56. if ((serverHost = gethostbyname(argv[1])) == 0) {
  57. perror("Please enter a valid host name.");
  58. exit(2);
  59. }
  60.  
  61. /* Create the clientSocket */
  62. if ((dataSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  63. perror("There was a problem creating the socket.");
  64. exit(3);
  65. }
  66. memset(&serverSocketAddress, 0, sizeof(serverSocketAddress));
  67. serverSocketAddress.sin_family = AF_INET;
  68. serverSocketAddress.sin_addr.s_addr = inet_addr(inet_ntoa(*((struct in_addr*) serverHost->h_addr_list[0]))); // Convert number_dot string to binary
  69. serverSocketAddress.sin_port = htons(SERV_PORT); // Host byte order -> Network byte order
  70. serverLength = sizeof(serverSocketAddress);
  71.  
  72. printf ("%s", "Please enter a string to send to the server: ");
  73. scanf("\n%[^\n]", string);
  74.  
  75. /* Get current timestamp and format for transport */
  76. gettimeofday(&sendTime, NULL);
  77. timestamp = htobe64((sendTime.tv_sec) * 1000L + (sendTime.tv_usec) / 1000);
  78. createPacket();
  79.  
  80. /* Send the data; clock the start time to milliseconds*/
  81. if (n = sendto(dataSocket, packet, PACKET_SIZE, 0, (struct sockaddr *) &serverSocketAddress, serverLength) < 0) {
  82. perror("Message send error");
  83. exit(4);
  84. }
  85.  
  86. /* Recieve the data; clock the recieve time to milliseonds; calculate RTT */
  87. if (n = recvfrom(dataSocket, packet, PACKET_SIZE, 0, (struct sockaddr *) &serverSocketAddress, &serverLength) < 0) {
  88. perror("The server terminated prematurely.");
  89. exit(4);
  90. }
  91.  
  92. /* Get current timestamp and get timestamp from the recv packet; calculate RTT */
  93. gettimeofday(&recvTime, NULL);
  94. recvTimeMS = (recvTime.tv_sec) * 1000L + (recvTime.tv_usec) / 1000;
  95. deconstructPacket();
  96. RTT = recvTimeMS - timestamp;
  97.  
  98. /* Output results */
  99. printf("%s", "String recieved from the server: ");
  100. printf("%s\n", string);
  101. printf("%s", "Round Trip Time: ");
  102. printf("%ld", RTT);
  103. printf("%s\n", " millisecond(s)");
  104.  
  105. return(0);
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement