Advertisement
VladimirKostovsky

OSI_Lab5_time

Dec 18th, 2023 (edited)
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <winsock2.h>
  3. #include <iphlpapi.h>
  4. #include <icmpapi.h>
  5. #include <stdio.h>
  6. #include <chrono>
  7.  
  8. #pragma comment(lib, "iphlpapi.lib")
  9. #pragma comment(lib, "ws2_32.lib")
  10. #pragma warning(disable : 4996)
  11.  
  12. using namespace std;
  13. using namespace std::chrono;
  14.  
  15. const int N = 512;
  16. const size_t timeout = 1000;
  17.  
  18. int MinMaxMed(int* arr, int size, int flag)
  19. {
  20.     if (flag == 1)
  21.     {
  22.         int min = timeout;
  23.         for (int i = 0; i < size; i++)
  24.             if (arr[i] < min)
  25.                 min = arr[i];
  26.         return min;
  27.     }
  28.     else if (flag == 2)
  29.     {
  30.         int max = 0;
  31.         for (int i = 0; i < size; i++)
  32.             if (arr[i] > max)
  33.                 max = arr[i];
  34.         return max;
  35.     }
  36.     else
  37.     {
  38.         int med = 0;
  39.         for (int i = 0; i < size; i++)
  40.             med += arr[i];
  41.         med /= size;
  42.         return med;
  43.     }
  44. }
  45.  
  46.  
  47. int main() {
  48.     WORD ver = MAKEWORD(2, 2);
  49.     WSADATA wsaData;
  50.     int err = 0;
  51.  
  52.     if (err = WSAStartup(ver, &wsaData))
  53.         cout << "Error while WSAStartup";
  54.  
  55.     HANDLE IcmpHandle;
  56.     char ReqData[N];
  57.     LPVOID ReplyBuffer;
  58.     WORD ReplySize;
  59.     char HostName[N];
  60.     unsigned long HostAddr;
  61.     DWORD dwRetVal;
  62.     int TrCount = 0;
  63.     int* RecTimeArr;
  64.  
  65.     cout << "Enter address to ping:\n";
  66.     cin >> HostName;
  67.     cout << "Enter tries count:\n";
  68.     cin >> TrCount;
  69.     cout << "Enter request message:\n";
  70.     cin >> ReqData;
  71.  
  72.     RecTimeArr = new int[TrCount] {};
  73.  
  74.     ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(ReqData) + 8;
  75.     ReplyBuffer = (VOID*)malloc(ReplySize);
  76.     if (ReplyBuffer == NULL) {
  77.         cout << "Unable to allocate memory\n";
  78.         return 1;
  79.     }
  80.  
  81.     IcmpHandle = IcmpCreateFile();
  82.     if (IcmpHandle == INVALID_HANDLE_VALUE) {
  83.         cout << "Error while opening handle\n";
  84.         return 1;
  85.     }
  86.  
  87.     struct hostent* host = gethostbyname(HostName);
  88.     if (host == NULL) {
  89.         cout << "Can't get host by name\n";
  90.         return 1;
  91.     }
  92.  
  93.     HostAddr = inet_addr(inet_ntoa(*((in_addr*)host->h_addr_list[0])));
  94.     for (int i = TrCount; i > 0; i--) {
  95.         auto start = high_resolution_clock::now();
  96.         dwRetVal = IcmpSendEcho2(IcmpHandle, nullptr, nullptr, nullptr, HostAddr, ReqData, sizeof(ReqData), nullptr, ReplyBuffer, ReplySize, timeout);
  97.         auto end = high_resolution_clock::now();
  98.         auto duration = duration_cast<milliseconds>(end - start);
  99.  
  100.         if (dwRetVal) {
  101.             PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
  102.             struct in_addr ReplyAddr;
  103.             ReplyAddr.S_un.S_addr = pEchoReply->Address;
  104.  
  105.             if (i == 1)
  106.                 cout << "Received " << dwRetVal << " icmp message from " << inet_ntoa(ReplyAddr) << " time: " << duration.count() << " ms\n";
  107.             else
  108.                 cout << "Received " << dwRetVal << " icmp messages from " << inet_ntoa(ReplyAddr) << " time: " << duration.count() << " ms\n";
  109.  
  110.             RecTimeArr[TrCount - i] = duration.count();
  111.         }
  112.         else {
  113.             cout << "Call to IcmpSendEcho2 failed.\n";
  114.         }
  115.     }
  116.  
  117.     if (RecTimeArr[0] != 0)
  118.         cout << "min - " << MinMaxMed(RecTimeArr, TrCount, 1) << " ms, max - " << MinMaxMed(RecTimeArr, TrCount, 2) << " ms, med - " << MinMaxMed(RecTimeArr, TrCount, 3) << " ms\n";
  119.  
  120.     free(ReplyBuffer);
  121.     IcmpCloseHandle(IcmpHandle);
  122.     WSACleanup();
  123.     delete[] RecTimeArr;
  124.  
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement