Advertisement
VladimirKostovsky

Osi_lab5_Echoes

Dec 18th, 2023 (edited)
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 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 <locale.h>
  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. const int N = 512;
  14. const size_t timeout = 1000;
  15. int MinMaxMed(int* arr, int size, int flag)
  16. {
  17.     if (flag == 1)
  18.     {
  19.         int min = timeout;
  20.         for (int i = 0; i < size; i++)
  21.             if (arr[i] < min)
  22.                 min = arr[i];
  23.         return min;
  24.     }
  25.     else if (flag == 2)
  26.     {
  27.         int max = 0;
  28.         for (int i = 0; i < size; i++)
  29.             if (arr[i] > max)
  30.                 max = arr[i];
  31.         return max;
  32.     }
  33.     else
  34.     {
  35.         int med = 0;
  36.         for (int i = 0; i < size; i++)
  37.             med += arr[i];
  38.         med /= size;
  39.         return med;
  40.     }
  41. }
  42.  
  43. int main()
  44. {
  45.     setlocale (LC_ALL, "Russian")
  46.     WORD ver = MAKEWORD(2, 2);
  47.     WSADATA wsaData;
  48.     int err = 0;
  49.     if (err = WSAStartup(ver, &wsaData))
  50.         cout << "Error while WSAStartup";
  51.  
  52.  
  53.  
  54.     HANDLE IcmpHandle;
  55.     char ReqData[N];
  56.     LPVOID ReplyBuffer;
  57.     WORD ReplySize;
  58.     char HostName[N];
  59.     unsigned long HostAddr;
  60.     DWORD dwRetVal;
  61.     int TrCount = 0;
  62.     int* RecTimeArr;
  63.     cout << "Введите адрес:\n";
  64.     cin >> HostName;
  65.     cout << "Введите количество попыток:\n";
  66.     cin >> TrCount;
  67.     cout << "Введите сообщение:\n";
  68.     cin >> ReqData;
  69.  
  70.     RecTimeArr = new int[TrCount] {};
  71.  
  72.     ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(ReqData) + 8;
  73.     ReplyBuffer = (VOID*)malloc(ReplySize);
  74.     if (ReplyBuffer == NULL)
  75.     {
  76.         cout << "Невозможно выделить память\n";
  77.         return 1;
  78.     }
  79.  
  80.     IcmpHandle = IcmpCreateFile();
  81.     if (IcmpHandle == INVALID_HANDLE_VALUE)
  82.     {
  83.         cout << "Ошибка при открытии \n";
  84.         return 1;
  85.     }
  86.  
  87.     struct hostent* host = gethostbyname(HostName);
  88.     if (host == NULL)
  89.     {
  90.         cout << "Can't get host by name\n";
  91.         return 1;
  92.     }
  93.  
  94.     HostAddr = inet_addr(inet_ntoa(*((in_addr*)host->h_addr_list[0])));
  95.     for (int i = TrCount; i > 0; i--)
  96.     {
  97.         dwRetVal = IcmpSendEcho(IcmpHandle, HostAddr, ReqData, sizeof(ReqData), NULL, ReplyBuffer, ReplySize, 1000);
  98.         if (dwRetVal)
  99.         {
  100.             PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
  101.             struct in_addr ReplyAddr;
  102.             ReplyAddr.S_un.S_addr = pEchoReply->Address;
  103.             if (i == 1)
  104.                 cout << "Received " << dwRetVal << " icmp message from " << inet_ntoa(ReplyAddr) << " time: " << pEchoReply->RoundTripTime << " ms\n";
  105.             else
  106.                 cout << "Received " << dwRetVal << " icmp messages from " << inet_ntoa(ReplyAddr) << " time: " << pEchoReply->RoundTripTime << " ms\n";
  107.             RecTimeArr[TrCount - i] = pEchoReply->RoundTripTime;
  108.         }
  109.         else
  110.         {
  111.             cout << "Call to IcmpSendEcho failed.\n";
  112.         }
  113.     }
  114.     if (RecTimeArr[0] != 0)
  115.         cout << "min - " << MinMaxMed(RecTimeArr, TrCount, 1) << " ms, max - " << MinMaxMed(RecTimeArr, TrCount, 2) << " ms, med - " << MinMaxMed(RecTimeArr, TrCount, 3) << " ms\n";
  116.  
  117.  
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement