Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.67 KB | None | 0 0
  1. #pragma comment(lib, "Ws2_32.lib")
  2. #pragma warning(disable: 4996)
  3.  
  4. using namespace std;
  5. #include <winsock2.h>
  6. #include <string.h>
  7. #include <iostream>
  8. #include <ctime>
  9.  
  10. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  11. #define TIME_PORT   27015
  12.  
  13. void CheckSentMsg(int bytes_sent, SOCKET& connSocket)
  14. {
  15.     if (SOCKET_ERROR == bytes_sent)
  16.     {
  17.         cout << "Time Client: Error at sendto(): " << WSAGetLastError() << endl;
  18.         closesocket(connSocket);
  19.         WSACleanup();
  20.         exit(1);
  21.     }
  22. }
  23.  
  24. void CheckReciveMsg(int bytes_recived, SOCKET& connSocket)
  25. {
  26.     if (SOCKET_ERROR == bytes_recived)
  27.     {
  28.         cout << "Time Client: Error at recv(): " << WSAGetLastError() << endl;
  29.         closesocket(connSocket);
  30.         WSACleanup();
  31.         exit(1);
  32.     }
  33. }
  34.  
  35. bool validateUserInput(char* user_input)
  36. {
  37.     if (strcmp(user_input, "1") == 0 || strcmp(user_input, "2") == 0 || strcmp(user_input, "3") == 0 ||
  38.         strcmp(user_input, "4") == 0 || strcmp(user_input, "5") == 0 || strcmp(user_input, "6") == 0 ||
  39.         strcmp(user_input, "7") == 0 || strcmp(user_input, "8") == 0 || strcmp(user_input, "9") == 0 ||
  40.         strcmp(user_input, "10") == 0 || strcmp(user_input, "11") == 0 || strcmp(user_input, "12") == 0)
  41.         return true;
  42.     else
  43.         return false;
  44. }
  45.  
  46. void ReciveMsgFromServer(SOCKET& connSocket)
  47. {
  48.     int bytesRecv;
  49.     char recvBuff[255];
  50.     bytesRecv = recv(connSocket, recvBuff, 255, 0);
  51.     CheckReciveMsg(bytesRecv, connSocket);
  52.     recvBuff[bytesRecv] = '\0'; //add the null-terminating to make it a string
  53.     cout << "Client: Recieved: " << bytesRecv << " bytes of \n\\" << recvBuff << "\\ message.\n\n";
  54. }
  55.  
  56. void GetClientToServerDelayEstimation(char* user_input, SOCKET& connSocket, sockaddr_in& server)
  57. {
  58.    
  59.     int bytesSent;
  60.     int tickCount[100];
  61.     int bytesRecv;
  62.     char recvBuff[255];
  63.     long double sum = 0;
  64.     for (int i = 0; i < 100; i++)
  65.     {
  66.         bytesSent = sendto(connSocket, user_input, (int)strlen(user_input), 0, (const sockaddr *)&server, sizeof(server));
  67.         CheckSentMsg(bytesSent, connSocket);
  68.     }
  69.     for (int i = 0; i < 100; i++)
  70.     {
  71.         bytesRecv = recv(connSocket, recvBuff, 255, 0);
  72.         CheckReciveMsg(bytesRecv, connSocket);
  73.         tickCount[i] = atoi(recvBuff);
  74.     }
  75.     for (int i = 0; i < 99; i++)
  76.     {
  77.         sum += tickCount[i + 1] - tickCount[i];
  78.     }
  79.     sum = sum / 99;
  80.     cout << "The delay estimation in milliseconds is: " << sum << endl << "in seconds: " << (sum / 1000) << endl;
  81. }
  82. void MeasureRTT(char* user_input, SOCKET& connSocket, sockaddr_in& server)
  83. {
  84.     int bytesSent;
  85.     int bytesRecv;
  86.     char recvBuff[255];
  87.     double RTTtime[100];
  88.     clock_t begin_time;
  89.     double sum = 0;
  90.     for (int i = 0; i < 100; i++)
  91.     {
  92.         bytesSent = sendto(connSocket, user_input, (int)strlen(user_input), 0, (const sockaddr *)&server, sizeof(server));
  93.         begin_time = clock();
  94.         CheckSentMsg(bytesSent, connSocket);
  95.         bytesRecv = recv(connSocket, recvBuff, 255, 0);
  96.         RTTtime[i] = clock() - begin_time;
  97.         CheckReciveMsg(bytesRecv, connSocket);
  98.     }
  99.     for (int i = 0; i < 100; i++)
  100.     {
  101.         sum += RTTtime[i];
  102.     }
  103.     sum /= 100;
  104.     cout << "The RTT Time in milliseconds is: " << sum << endl << "in seconds: " << (sum / 1000) << endl;
  105. }
  106.  
  107. void ActAccordingToInput(char* user_input, SOCKET& connSocket, sockaddr_in& server)
  108. {
  109.     if (strcmp(user_input, "12") != 0)
  110.     {
  111.         if (strcmp(user_input, "4") == 0)
  112.         {
  113.             GetClientToServerDelayEstimation(user_input, connSocket, server);
  114.         }
  115.         if (strcmp(user_input, "5") == 0)
  116.         {
  117.             MeasureRTT(user_input, connSocket, server);
  118.         }
  119.         else
  120.         {
  121.             int bytesSent;
  122.             bytesSent = sendto(connSocket, user_input, (int)strlen(user_input), 0, (const sockaddr *)&server, sizeof(server));
  123.             CheckSentMsg(bytesSent, connSocket);
  124.             cout << "Time Client: Sent: " << bytesSent << "/" << strlen(user_input) << " bytes of \"" << user_input << "\" message.\n";
  125.             ReciveMsgFromServer(connSocket);
  126.         }
  127.     }
  128.     else
  129.         return;
  130. }
  131.  
  132. int main()
  133. {
  134.     // Initialize Winsock (Windows Sockets).
  135.     WSAData wsaData;
  136.     if (NO_ERROR != WSAStartup(MAKEWORD(2, 2), &wsaData))
  137.     {
  138.         cout << "Time Client: Error at WSAStartup()\n";
  139.         return 0;
  140.     }
  141.     // Client side:
  142.     // Create a socket and connect to an internet address.
  143.     SOCKET connSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  144.     if (INVALID_SOCKET == connSocket)
  145.     {
  146.         cout << "Time Client: Error at socket(): " << WSAGetLastError() << endl;
  147.         WSACleanup();
  148.         return 0;
  149.     }
  150.  
  151.     sockaddr_in server;
  152.     server.sin_family = AF_INET;
  153.     server.sin_addr.s_addr = inet_addr("127.0.0.1");
  154.     server.sin_port = htons(TIME_PORT);
  155.  
  156.     char UserInput[255] = "";
  157.     bool validatedUserInput = true;
  158.     do
  159.     {
  160.         cout << "What would you like to ask the server?\n";
  161.         cout << "Enter 1 to Ask the time\n"
  162.             << "Enter 2 to Ask the time without the date\n"
  163.             << "Enter 3 to Ask the time since Epoch\n"
  164.             << "Enter 4 to Ask the client to server delay estimation\n"
  165.             << "Enter 5 to Ask the Measure RTT\n"
  166.             << "Enter 6 to Ask the time without date or seconds\n"
  167.             << "Enter 7 to Ask the year\n"
  168.             << "Enter 8 to Ask the month & day\n"
  169.             << "Enter 9 to Ask the seconds since begining of the month\n"
  170.             << "Enter 10 to Ask the days since the beginning of the year\n"
  171.             << "Enter 11 to Ask if it is day light saving time\n"
  172.             << "Enter 12 to exit\n";
  173.         cin >> UserInput;
  174.         validatedUserInput = validateUserInput(UserInput);
  175.         if (validatedUserInput)
  176.             ActAccordingToInput(UserInput, connSocket , server);
  177.         else
  178.             cout << "The input that was given is not recognized, please try again\n";
  179.     } while (strcmp(UserInput, "12") != 0);
  180.  
  181.     // Closing connections and Winsock.
  182.     cout << "Time Client: Closing Connection.\n";
  183.     closesocket(connSocket);
  184.  
  185.     system("pause");
  186.     return 0;
  187. }
  188.  
  189. /*
  190. #pragma warning(disable: 4996)
  191.  
  192.     while (UserChoise != 4)
  193.     {
  194.         cout << "What would you like to ask the server?\n";
  195.         cout << "1) Ask the time?\n";
  196.         cout << "2) Ask the server how are you?\n";
  197.         cout << "3) Ask the server the wether?\n";
  198.         cout << "4) exit\n";
  199.         cin >> UserChoise;
  200.         if (UserChoise == 4) { return; }
  201.         if (UserChoise == 1)
  202.         {
  203.             strcpy(sendBuff, "1");
  204.             bytesSent = sendto(connSocket, sendBuff, (int)strlen(sendBuff), 0, (const sockaddr *)&server, sizeof(server));
  205.             if (SOCKET_ERROR == bytesSent)
  206.             {
  207.                 cout << "Time Client: Error at sendto(): " << WSAGetLastError() << endl;
  208.                 closesocket(connSocket);
  209.                 WSACleanup();
  210.                 return;
  211.             }
  212.             cout << "Time Client: Sent: " << bytesSent << "/" << strlen(sendBuff) << " bytes of \"" << sendBuff << "\" message.\n";
  213.         }
  214.         if (UserChoise == 2)
  215.         {
  216.             strcpy(sendBuff, "2");
  217.             bytesSent = sendto(connSocket, sendBuff, (int)strlen(sendBuff), 0, (const sockaddr *)&server, sizeof(server));
  218.             if (SOCKET_ERROR == bytesSent)
  219.             {
  220.                 cout << "Time Client: Error at sendto(): " << WSAGetLastError() << endl;
  221.                 closesocket(connSocket);
  222.                 WSACleanup();
  223.                 return;
  224.             }
  225.             cout << "Time Client: Sent: " << bytesSent << "/" << strlen(sendBuff) << " bytes of \"" << sendBuff << "\" message.\n";
  226.         }
  227.         if (UserChoise == 3)
  228.         {
  229.             strcpy(sendBuff, "3");
  230.             bytesSent = sendto(connSocket, sendBuff, (int)strlen(sendBuff), 0, (const sockaddr *)&server, sizeof(server));
  231.             if (SOCKET_ERROR == bytesSent)
  232.             {
  233.                 cout << "Time Client: Error at sendto(): " << WSAGetLastError() << endl;
  234.                 closesocket(connSocket);
  235.                 WSACleanup();
  236.                 return;
  237.             }
  238.             cout << "Time Client: Sent: " << bytesSent << "/" << strlen(sendBuff) << " bytes of \"" << sendBuff << "\" message.\n";
  239.         }
  240.  
  241.         bytesRecv = recv(connSocket, recvBuff, 255, 0);
  242.         if (SOCKET_ERROR == bytesRecv)
  243.         {
  244.             cout << "Time Client: Error at recv(): " << WSAGetLastError() << endl;
  245.             closesocket(connSocket);
  246.             WSACleanup();
  247.             return;
  248.         }
  249.         recvBuff[bytesRecv] = '\0'; //add the null-terminating to make it a string
  250.         cout << "Time Client: Recieved: " << bytesRecv << " bytes of \"" << recvBuff << "\" message.\n";
  251.     }
  252.  
  253.  
  254. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement