Xom9ik

Lab_6/15var (IV semester) CN

Apr 29th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.02 KB | None | 0 0
  1. //MainForm.cpp
  2. #include "MainForm.h"
  3. using namespace System;
  4. using namespace System::Windows::Forms;
  5. [STAThreadAttribute]
  6. void Main(array<String^>^ args) {
  7.     Application::EnableVisualStyles();
  8.     Application::SetCompatibleTextRenderingDefault(false);
  9.     WinForms_CN::MainForm form;
  10.     Application::Run(%form);
  11. }
  12. //MainForm.h
  13. #pragma once
  14. #include <windows.h>
  15. #include <iphlpapi.h>
  16. #include <icmpapi.h>
  17. #include <string>
  18.  
  19.  
  20. #pragma comment(lib, "ws2_32.lib")
  21. #pragma comment(lib, "IPHLPAPI.lib")
  22.  
  23. namespace WinForms_CN {
  24.  
  25.     using namespace System;
  26.     using namespace System::ComponentModel;
  27.     using namespace System::Collections;
  28.     using namespace System::Windows::Forms;
  29.     using namespace System::Data;
  30.     using namespace System::Drawing;
  31.     using namespace System::Runtime::InteropServices;
  32.     public ref class MainForm : public System::Windows::Forms::Form
  33.     {
  34.     public:
  35.         MainForm(void)
  36.         {
  37.             InitializeComponent();
  38.         }
  39.  
  40.     protected:
  41.         ~MainForm()
  42.         {
  43.             if (components)
  44.             {
  45.                 delete components;
  46.             }
  47.         }
  48.     private: System::Windows::Forms::Button^  button1;
  49.     private: System::Windows::Forms::TextBox^  textBox1;
  50.     private: System::Windows::Forms::Button^  button2;
  51.     private: System::Windows::Forms::TextBox^  textBox2;
  52.     private: System::Windows::Forms::TextBox^  textBox3;
  53.     private: System::Windows::Forms::TextBox^  textBox4;
  54.     private: System::Windows::Forms::Label^  label1;
  55.     private: System::Windows::Forms::Label^  label2;
  56.     private: System::Windows::Forms::Label^  label3;
  57.  
  58.  
  59.     protected:
  60.  
  61.     private:
  62.         System::ComponentModel::Container ^components;
  63.  
  64. #pragma region Windows Form Designer generated code
  65.         void InitializeComponent(void)
  66.         {
  67.             this->button1 = (gcnew System::Windows::Forms::Button());
  68.             this->textBox1 = (gcnew System::Windows::Forms::TextBox());
  69.             this->button2 = (gcnew System::Windows::Forms::Button());
  70.             this->textBox2 = (gcnew System::Windows::Forms::TextBox());
  71.             this->textBox3 = (gcnew System::Windows::Forms::TextBox());
  72.             this->textBox4 = (gcnew System::Windows::Forms::TextBox());
  73.             this->label1 = (gcnew System::Windows::Forms::Label());
  74.             this->label2 = (gcnew System::Windows::Forms::Label());
  75.             this->label3 = (gcnew System::Windows::Forms::Label());
  76.             this->SuspendLayout();
  77.             //
  78.             // button1
  79.             //
  80.             this->button1->Location = System::Drawing::Point(342, 25);
  81.             this->button1->Name = L"button1";
  82.             this->button1->Size = System::Drawing::Size(70, 23);
  83.             this->button1->TabIndex = 0;
  84.             this->button1->Text = L"Ping";
  85.             this->button1->UseVisualStyleBackColor = true;
  86.             this->button1->Click += gcnew System::EventHandler(this, &MainForm::button1_Click);
  87.             //
  88.             // textBox1
  89.             //
  90.             this->textBox1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
  91.                 | System::Windows::Forms::AnchorStyles::Left)
  92.                 | System::Windows::Forms::AnchorStyles::Right));
  93.             this->textBox1->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
  94.                 static_cast<System::Byte>(204)));
  95.             this->textBox1->Location = System::Drawing::Point(12, 97);
  96.             this->textBox1->Multiline = true;
  97.             this->textBox1->Name = L"textBox1";
  98.             this->textBox1->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
  99.             this->textBox1->Size = System::Drawing::Size(400, 349);
  100.             this->textBox1->TabIndex = 1;
  101.             //
  102.             // button2
  103.             //
  104.             this->button2->Location = System::Drawing::Point(342, 68);
  105.             this->button2->Name = L"button2";
  106.             this->button2->Size = System::Drawing::Size(70, 23);
  107.             this->button2->TabIndex = 2;
  108.             this->button2->Text = L"Close";
  109.             this->button2->UseVisualStyleBackColor = true;
  110.             this->button2->Click += gcnew System::EventHandler(this, &MainForm::button2_Click);
  111.             //
  112.             // textBox2
  113.             //
  114.             this->textBox2->Location = System::Drawing::Point(12, 28);
  115.             this->textBox2->Name = L"textBox2";
  116.             this->textBox2->Size = System::Drawing::Size(271, 20);
  117.             this->textBox2->TabIndex = 3;
  118.             this->textBox2->Text = L"4pda.ru";
  119.             //
  120.             // textBox3
  121.             //
  122.             this->textBox3->Location = System::Drawing::Point(15, 71);
  123.             this->textBox3->Name = L"textBox3";
  124.             this->textBox3->Size = System::Drawing::Size(106, 20);
  125.             this->textBox3->TabIndex = 4;
  126.             this->textBox3->Text = L"50";
  127.             //
  128.             // textBox4
  129.             //
  130.             this->textBox4->Location = System::Drawing::Point(177, 71);
  131.             this->textBox4->Name = L"textBox4";
  132.             this->textBox4->Size = System::Drawing::Size(106, 20);
  133.             this->textBox4->TabIndex = 5;
  134.             this->textBox4->Text = L"8192";
  135.             //
  136.             // label1
  137.             //
  138.             this->label1->AutoSize = true;
  139.             this->label1->Location = System::Drawing::Point(12, 12);
  140.             this->label1->Name = L"label1";
  141.             this->label1->Size = System::Drawing::Size(96, 13);
  142.             this->label1->TabIndex = 6;
  143.             this->label1->Text = L"Remote host name";
  144.             //
  145.             // label2
  146.             //
  147.             this->label2->AutoSize = true;
  148.             this->label2->Location = System::Drawing::Point(12, 55);
  149.             this->label2->Name = L"label2";
  150.             this->label2->Size = System::Drawing::Size(106, 13);
  151.             this->label2->TabIndex = 7;
  152.             this->label2->Text = L"Number of packages";
  153.             //
  154.             // label3
  155.             //
  156.             this->label3->AutoSize = true;
  157.             this->label3->Location = System::Drawing::Point(176, 55);
  158.             this->label3->Name = L"label3";
  159.             this->label3->Size = System::Drawing::Size(71, 13);
  160.             this->label3->TabIndex = 8;
  161.             this->label3->Text = L"Package size";
  162.             //
  163.             // MainForm
  164.             //
  165.             this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  166.             this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  167.             this->ClientSize = System::Drawing::Size(424, 461);
  168.             this->Controls->Add(this->label3);
  169.             this->Controls->Add(this->label2);
  170.             this->Controls->Add(this->label1);
  171.             this->Controls->Add(this->textBox4);
  172.             this->Controls->Add(this->textBox3);
  173.             this->Controls->Add(this->textBox2);
  174.             this->Controls->Add(this->button2);
  175.             this->Controls->Add(this->textBox1);
  176.             this->Controls->Add(this->button1);
  177.             this->Name = L"MainForm";
  178.             this->Text = L"Lab_6 CN";
  179.             this->ResumeLayout(false);
  180.             this->PerformLayout();
  181.  
  182.         }
  183. #pragma endregion
  184.  
  185.    
  186.     private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
  187.         int numberOfPackages = int::Parse(textBox3->Text);
  188.         int packageSize = int::Parse(textBox4->Text);
  189.  
  190.         int totalSum = 0;
  191.  
  192.         WSADATA wsaData;
  193.         int iResult;
  194.         struct hostent *remoteHost;
  195.         char *host_name;
  196.         struct in_addr addr;
  197.         iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  198.         String ^name = Convert::ToString(textBox2->Text);
  199.         IntPtr ptrToNativeString = Marshal::StringToHGlobalAnsi(name);
  200.         host_name = static_cast<char*>(ptrToNativeString.ToPointer());
  201.         remoteHost = gethostbyname(host_name);
  202.         textBox1->Text += "HOSTNAME: " + gcnew String(host_name) + "\r\n";
  203.  
  204.         addr.s_addr = *(u_long *)remoteHost->h_addr_list[0];
  205.         std::string ip_add = inet_ntoa(addr);
  206.         textBox1->Text += "IP: " + gcnew String(inet_ntoa(addr)) + "\r\n";
  207.  
  208.         for (int i = 0; i < numberOfPackages; i++) {
  209.             HANDLE hIcmpFile = IcmpCreateFile();
  210.             if (hIcmpFile != INVALID_HANDLE_VALUE)
  211.             {
  212.                 DWORD ReplySize = sizeof(ICMP_ECHO_REPLY) + packageSize;
  213.                 char *ReplyBuffer = new char[ReplySize];
  214.                 const int addr = inet_addr(ip_add.c_str());
  215.                 if (addr != INADDR_NONE) {
  216.                     if (IcmpSendEcho(hIcmpFile, addr, new char[packageSize], packageSize, 0, ReplyBuffer, ReplySize, 1000) != 0) {
  217.                         PICMP_ECHO_REPLY icmpReply = (PICMP_ECHO_REPLY)ReplyBuffer;
  218.                         totalSum += icmpReply->RoundTripTime;
  219.                     }
  220.                 }
  221.                 delete[]ReplyBuffer;
  222.                 IcmpCloseHandle(hIcmpFile);
  223.             }
  224.         }
  225.         textBox1->Text += "Send packages: " + numberOfPackages + "\r\n";
  226.         textBox1->Text += "Package size: " + packageSize + "\r\n";
  227.         textBox1->Text += "Average ping: " + (double)totalSum / numberOfPackages + "\r\n";
  228.         textBox1->Text += "*****************************************************\r\n\r\n";
  229.     }
  230.    
  231. private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
  232.     Application::Exit();
  233. }
  234. };
  235. }
Add Comment
Please, Sign In to add comment