Xom9ik

Lab_7/15var (IV semester) CN

Jun 1st, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.45 KB | None | 0 0
  1. //MainForm.cpp
  2. #include "MainForm.h"
  3. using namespace System;
  4. using namespace System::Windows::Forms;
  5.  
  6. [STAThreadAttribute]
  7. void Main(array<String^>^ args) {
  8.     Application::EnableVisualStyles();
  9.     Application::SetCompatibleTextRenderingDefault(false);
  10.     WinForms_CN::MainForm form;
  11.     Application::Run(%form);
  12. }
  13.  
  14. //MainForm.h
  15. #pragma once
  16. #include <winsock2.h>
  17. #include <ws2tcpip.h>
  18. #include <iostream>
  19. #include <string>
  20. #include <msclr\marshal_cppstd.h>
  21. #pragma comment (lib, "Ws2_32.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.     using namespace System::Threading;
  33.  
  34.     public struct client_type
  35.     {
  36.         SOCKET socket;
  37.         int id;
  38.         char received_message[512];
  39.     };
  40.     public struct client_type_connect
  41.     {
  42.         SOCKET socket;
  43.         int id;
  44.     };
  45.         struct addrinfo *result = NULL, *ptr = NULL, hints;
  46.     std::string host;
  47.     int remotePort;
  48.     int localPort;
  49.  
  50.     const char OPTION_VALUE = 1;
  51.     const int MAX_CLIENTS = 5;
  52.     std::string old = "";
  53.  
  54.     std::vector<client_type_connect> client_array(MAX_CLIENTS);
  55.     client_type_connect &new_client = client_array[0];
  56.  
  57.     public ref class MainForm : public System::Windows::Forms::Form
  58.     {
  59.  
  60.     public:
  61.         MainForm(void)
  62.         {
  63.             InitializeComponent();
  64.         }
  65.        
  66.     protected:
  67.         ~MainForm()
  68.         {
  69.             if (components)
  70.             {
  71.                 delete components;
  72.             }
  73.         }
  74.     private: System::Windows::Forms::Button^  button1;
  75.     private: System::Windows::Forms::TextBox^  textBox1;
  76.     private: System::Windows::Forms::Button^  button2;
  77.     private: System::Windows::Forms::TextBox^  textBox2;
  78.     private: System::Windows::Forms::TextBox^  textBox3;
  79.     private: System::Windows::Forms::TextBox^  textBox4;
  80.     private: System::Windows::Forms::Label^  label1;
  81.     private: System::Windows::Forms::Label^  label2;
  82.     private: System::Windows::Forms::Label^  label3;
  83.     private: System::Windows::Forms::TextBox^  textBox5;
  84.     private: System::Windows::Forms::Button^  button3;
  85.  
  86.  
  87.     protected:
  88.  
  89.     private:
  90.         System::ComponentModel::Container ^components;
  91.  
  92.         delegate void UpdateTextBox1delegate(System::String^ someText);
  93.  
  94.         void Update(System::String^ someText) {
  95.  
  96.             if (this->textBox1->InvokeRequired) {
  97.  
  98.                 UpdateTextBox1delegate^ d = gcnew UpdateTextBox1delegate(this, &MainForm::Update);
  99.                 this->Invoke(d, gcnew array<Object^> { someText });
  100.             }
  101.             else {
  102.                 this->textBox1->Text += someText + "\r\n";
  103.             }
  104.         }
  105.  
  106. #pragma region Windows Form Designer generated code
  107.         void InitializeComponent(void)
  108.         {
  109.             this->button1 = (gcnew System::Windows::Forms::Button());
  110.             this->textBox1 = (gcnew System::Windows::Forms::TextBox());
  111.             this->button2 = (gcnew System::Windows::Forms::Button());
  112.             this->textBox2 = (gcnew System::Windows::Forms::TextBox());
  113.             this->textBox3 = (gcnew System::Windows::Forms::TextBox());
  114.             this->textBox4 = (gcnew System::Windows::Forms::TextBox());
  115.             this->label1 = (gcnew System::Windows::Forms::Label());
  116.             this->label2 = (gcnew System::Windows::Forms::Label());
  117.             this->label3 = (gcnew System::Windows::Forms::Label());
  118.             this->textBox5 = (gcnew System::Windows::Forms::TextBox());
  119.             this->button3 = (gcnew System::Windows::Forms::Button());
  120.             this->SuspendLayout();
  121.             //
  122.             // button1
  123.             //
  124.             this->button1->Location = System::Drawing::Point(404, 45);
  125.             this->button1->Name = L"button1";
  126.             this->button1->Size = System::Drawing::Size(70, 23);
  127.             this->button1->TabIndex = 0;
  128.             this->button1->Text = L"Change";
  129.             this->button1->UseVisualStyleBackColor = true;
  130.             this->button1->Click += gcnew System::EventHandler(this, &MainForm::button1_Click);
  131.             //
  132.             // textBox1
  133.             //
  134.             this->textBox1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
  135.                 | System::Windows::Forms::AnchorStyles::Left)
  136.                 | System::Windows::Forms::AnchorStyles::Right));
  137.             this->textBox1->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
  138.                 static_cast<System::Byte>(204)));
  139.             this->textBox1->Location = System::Drawing::Point(12, 97);
  140.             this->textBox1->Multiline = true;
  141.             this->textBox1->Name = L"textBox1";
  142.             this->textBox1->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
  143.             this->textBox1->Size = System::Drawing::Size(732, 283);
  144.             this->textBox1->TabIndex = 1;
  145.             //
  146.             // button2
  147.             //
  148.             this->button2->Location = System::Drawing::Point(480, 45);
  149.             this->button2->Name = L"button2";
  150.             this->button2->Size = System::Drawing::Size(70, 23);
  151.             this->button2->TabIndex = 2;
  152.             this->button2->Text = L"Clear";
  153.             this->button2->UseVisualStyleBackColor = true;
  154.             this->button2->Click += gcnew System::EventHandler(this, &MainForm::button2_Click);
  155.             //
  156.             // textBox2
  157.             //
  158.             this->textBox2->Location = System::Drawing::Point(118, 9);
  159.             this->textBox2->Name = L"textBox2";
  160.             this->textBox2->Size = System::Drawing::Size(154, 20);
  161.             this->textBox2->TabIndex = 3;
  162.             this->textBox2->Text = L"localhost";
  163.             //
  164.             // textBox3
  165.             //
  166.             this->textBox3->Location = System::Drawing::Point(148, 45);
  167.             this->textBox3->Name = L"textBox3";
  168.             this->textBox3->Size = System::Drawing::Size(124, 20);
  169.             this->textBox3->TabIndex = 4;
  170.             this->textBox3->Text = L"5000";
  171.             //
  172.             // textBox4
  173.             //
  174.             this->textBox4->Location = System::Drawing::Point(400, 9);
  175.             this->textBox4->Name = L"textBox4";
  176.             this->textBox4->Size = System::Drawing::Size(150, 20);
  177.             this->textBox4->TabIndex = 5;
  178.             this->textBox4->Text = L"5000";
  179.             //
  180.             // label1
  181.             //
  182.             this->label1->AutoSize = true;
  183.             this->label1->Location = System::Drawing::Point(12, 12);
  184.             this->label1->Name = L"label1";
  185.             this->label1->Size = System::Drawing::Size(100, 13);
  186.             this->label1->TabIndex = 6;
  187.             this->label1->Text = L"Remote node name";
  188.             //
  189.             // label2
  190.             //
  191.             this->label2->AutoSize = true;
  192.             this->label2->Location = System::Drawing::Point(12, 48);
  193.             this->label2->Name = L"label2";
  194.             this->label2->Size = System::Drawing::Size(130, 13);
  195.             this->label2->TabIndex = 7;
  196.             this->label2->Text = L"Remote node port number";
  197.             //
  198.             // label3
  199.             //
  200.             this->label3->AutoSize = true;
  201.             this->label3->Location = System::Drawing::Point(302, 12);
  202.             this->label3->Name = L"label3";
  203.             this->label3->Size = System::Drawing::Size(92, 13);
  204.             this->label3->TabIndex = 8;
  205.             this->label3->Text = L"Local port number";
  206.             //
  207.             // textBox5
  208.             //
  209.             this->textBox5->Anchor = static_cast<System::Windows::Forms::AnchorStyles>(((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left)
  210.                 | System::Windows::Forms::AnchorStyles::Right));
  211.             this->textBox5->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
  212.                 static_cast<System::Byte>(204)));
  213.             this->textBox5->Location = System::Drawing::Point(12, 386);
  214.             this->textBox5->Multiline = true;
  215.             this->textBox5->Name = L"textBox5";
  216.             this->textBox5->Size = System::Drawing::Size(667, 66);
  217.             this->textBox5->TabIndex = 9;
  218.             //
  219.             // button3
  220.             //
  221.             this->button3->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
  222.             this->button3->Location = System::Drawing::Point(682, 386);
  223.             this->button3->Name = L"button3";
  224.             this->button3->Size = System::Drawing::Size(62, 66);
  225.             this->button3->TabIndex = 10;
  226.             this->button3->Text = L"Send";
  227.             this->button3->UseVisualStyleBackColor = true;
  228.             this->button3->Click += gcnew System::EventHandler(this, &MainForm::button3_Click);
  229.             //
  230.             // MainForm
  231.             //
  232.             this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  233.             this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  234.             this->ClientSize = System::Drawing::Size(755, 459);
  235.             this->Controls->Add(this->button3);
  236.             this->Controls->Add(this->textBox5);
  237.             this->Controls->Add(this->label3);
  238.             this->Controls->Add(this->label2);
  239.             this->Controls->Add(this->label1);
  240.             this->Controls->Add(this->textBox4);
  241.             this->Controls->Add(this->textBox3);
  242.             this->Controls->Add(this->textBox2);
  243.             this->Controls->Add(this->button2);
  244.             this->Controls->Add(this->textBox1);
  245.             this->Controls->Add(this->button1);
  246.             this->Name = L"MainForm";
  247.             this->Text = L"Lab_7 CN";
  248.             this->Load += gcnew System::EventHandler(this, &MainForm::MainForm_Load);
  249.             this->ResumeLayout(false);
  250.             this->PerformLayout();
  251.  
  252.         }
  253. #pragma endregion
  254.  
  255.  
  256.        
  257.         void Send() {
  258.             WSADATA wsa_data;
  259.  
  260.             std::string sent_message = "";
  261.             client_type client = { INVALID_SOCKET, -1, "" };
  262.             int iResult = 0;
  263.             std::string message;
  264.  
  265.  
  266.             // Initialize Winsock
  267.             iResult = WSAStartup(MAKEWORD(2, 2), &wsa_data);
  268.            
  269.             ZeroMemory(&hints, sizeof(hints));
  270.             hints.ai_family = AF_UNSPEC;
  271.             hints.ai_socktype = SOCK_STREAM;
  272.             hints.ai_protocol = IPPROTO_TCP;
  273.  
  274.  
  275.             // Resolve the server address and port
  276.             std::string tmp = std::to_string(localPort);
  277.  
  278.             iResult = getaddrinfo(host.c_str(), tmp.c_str(), &hints, &result);
  279.             if (iResult != 0) {
  280.                 WSACleanup();
  281.             }
  282.  
  283.             for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
  284.  
  285.                 client.socket = socket(ptr->ai_family, ptr->ai_socktype,
  286.                     ptr->ai_protocol);
  287.                 if (client.socket == INVALID_SOCKET) {
  288.                     WSACleanup();
  289.                 }
  290.  
  291.                 iResult = connect(client.socket, ptr->ai_addr, (int)ptr->ai_addrlen);
  292.                 if (iResult == SOCKET_ERROR) {
  293.                     closesocket(client.socket);
  294.                     client.socket = INVALID_SOCKET;
  295.                     continue;
  296.                 }
  297.                 break;
  298.             }
  299.  
  300.             freeaddrinfo(result);
  301.  
  302.             if (client.socket == INVALID_SOCKET) {
  303.                 WSACleanup();
  304.             }
  305.             recv(client.socket, client.received_message, 512, 0);
  306.             message = client.received_message;
  307.  
  308.             if (message != "Server is full")
  309.             {
  310.                 client.id = atoi(client.received_message);
  311.  
  312.                 sent_message = msclr::interop::marshal_as<std::string>(textBox5->Text);
  313.                 new_client.socket = client.socket;
  314.  
  315.                 iResult = send(client.socket, sent_message.c_str(), strlen(sent_message.c_str()), 0);
  316.                 /*std::string tttmp = "Send to socket #" + std::to_string(client.socket) + ": " + sent_message;
  317.                 MessageBox::Show(gcnew String(tttmp.c_str()));*/
  318.             }
  319.            
  320.            
  321.             iResult = shutdown(client.socket, SD_SEND);
  322.             if (iResult == SOCKET_ERROR) {
  323.                 closesocket(client.socket);
  324.                 WSACleanup();
  325.             }
  326.  
  327.             closesocket(client.socket);
  328.             WSACleanup();
  329.         }
  330.        
  331.         void process_client_connect()
  332.         {
  333.             std::string msg = "";
  334.             old = "";
  335.             char tempmsg[512] = "";
  336.             String^ mes = "***Connection established***\r\n"
  337.                 + "Remote host: "
  338.                 + gcnew String(host.c_str()) + "\n";
  339.             this->Invoke(gcnew UpdateTextBox1delegate(this, &MainForm::Update), gcnew array<Object^> { mes });
  340.  
  341.             memset(tempmsg, 0, 512);
  342.  
  343.             int iResult = recv(new_client.socket, tempmsg, 512, 0);
  344.  
  345.             strcmp("", tempmsg);
  346.             msg = "SOCKET #" + std::to_string(new_client.socket) + ": " + tempmsg;
  347.             std::cout << msg.c_str() << std::endl;
  348.  
  349.             String^ message = gcnew String(msg.c_str());
  350.             this->Invoke(gcnew UpdateTextBox1delegate(this, &MainForm::Update), gcnew array<Object^> { message });
  351.  
  352.  
  353.             for (int i = 0; i < MAX_CLIENTS; i++)
  354.             {
  355.                 if (client_array[i].socket != INVALID_SOCKET)
  356.                     if (new_client.id != i)
  357.                         iResult = send(client_array[i].socket, msg.c_str(), strlen(msg.c_str()), 0);
  358.             }
  359.             msg = "Client #" + std::to_string(new_client.id) + " Disconnected";
  360.             mes = "***Connection broken***\r\n\r\n";
  361.             this->Invoke(gcnew UpdateTextBox1delegate(this, &MainForm::Update), gcnew array<Object^> { mes });
  362.  
  363.             closesocket(new_client.socket);
  364.             closesocket(client_array[new_client.id].socket);
  365.             client_array[new_client.id].socket = INVALID_SOCKET;
  366.  
  367.             for (int i = 0; i < MAX_CLIENTS; i++)
  368.             {
  369.                 if (client_array[i].socket != INVALID_SOCKET)
  370.                     iResult = send(client_array[i].socket, msg.c_str(), strlen(msg.c_str()), 0);
  371.             }
  372.            
  373.         }
  374.        
  375.         void Listening() {
  376.             host = msclr::interop::marshal_as<std::string>(textBox2->Text);
  377.             remotePort = Convert::ToInt32(textBox3->Text);
  378.             localPort = Convert::ToInt32(textBox4->Text);
  379.             //host = "localhost";
  380.             //localPort = 5000;
  381.             WSADATA wsaData;
  382.             struct addrinfo hints;
  383.             struct addrinfo *server = NULL;
  384.             SOCKET server_socket = INVALID_SOCKET;
  385.             std::string msg = "";
  386.             std::vector<client_type_connect> client(MAX_CLIENTS);
  387.             int num_clients = 0;
  388.             int temp_id = -1;
  389.  
  390.             WSAStartup(MAKEWORD(2, 2), &wsaData);
  391.  
  392.             ZeroMemory(&hints, sizeof(hints));
  393.             hints.ai_family = AF_INET;
  394.             hints.ai_socktype = SOCK_STREAM;
  395.             hints.ai_protocol = IPPROTO_TCP;
  396.             hints.ai_flags = AI_PASSIVE;
  397.  
  398.  
  399.             std::string tmp = std::to_string(localPort);
  400.  
  401.             getaddrinfo(host.c_str(), tmp.c_str(), &hints, &server);
  402.  
  403.             server_socket = socket(server->ai_family, server->ai_socktype, server->ai_protocol);
  404.  
  405.             setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &OPTION_VALUE, sizeof(int));
  406.             setsockopt(server_socket, IPPROTO_TCP, TCP_NODELAY, &OPTION_VALUE, sizeof(int));
  407.  
  408.             bind(server_socket, server->ai_addr, (int)server->ai_addrlen);
  409.  
  410.             MessageBox::Show("Listening...");
  411.  
  412.             listen(server_socket, SOMAXCONN);
  413.  
  414.             for (int i = 0; i < MAX_CLIENTS; i++)
  415.             {
  416.                 client[i].socket = -1;
  417.                 client[i].id = INVALID_SOCKET;
  418.             }
  419.  
  420.             while (1)
  421.             {
  422.  
  423.                 SOCKET incoming = INVALID_SOCKET;
  424.                 incoming = accept(server_socket, NULL, NULL);
  425.                 if (incoming == INVALID_SOCKET) continue;
  426.  
  427.                 num_clients = -1;
  428.  
  429.                 temp_id = -1;
  430.                 for (int i = 0; i < MAX_CLIENTS; i++)
  431.                 {
  432.                     if (client[i].socket == INVALID_SOCKET && temp_id == -1)
  433.                     {
  434.                         client[i].socket = incoming;
  435.                         client[i].id = i;
  436.                         temp_id = i;
  437.                     }
  438.                     if (client[i].socket != INVALID_SOCKET)
  439.                         num_clients++;
  440.                 }
  441.                 if (temp_id != -1)
  442.                 {
  443.                     msg = std::to_string(client[temp_id].id);
  444.                     send(client[temp_id].socket, msg.c_str(), strlen(msg.c_str()), 0);
  445.                     new_client = client[temp_id];
  446.                     client_array = client;
  447.                     Thread^ threadProcess = gcnew Thread(gcnew ThreadStart(this, &MainForm::process_client_connect));
  448.                     threadProcess->Start();
  449.                 }
  450.                 else
  451.                 {
  452.                     msg = "Server is full";
  453.                     send(incoming, msg.c_str(), strlen(msg.c_str()), 0);
  454.                 }
  455.             }
  456.             closesocket(server_socket);
  457.             for (int i = 0; i < MAX_CLIENTS; i++)
  458.                 closesocket(client[i].socket);
  459.             WSACleanup();
  460.         }
  461.        
  462.  
  463.  
  464.        
  465.         // Change
  466.     private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
  467.         host = msclr::interop::marshal_as<std::string>(textBox2->Text);
  468.         remotePort = Convert::ToInt32(textBox3->Text);
  469.         localPort = Convert::ToInt32(textBox4->Text);
  470.     }
  471.  
  472.              // Clear
  473.     private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
  474.         textBox1->Text = "";
  475.        
  476.     }
  477.  
  478.              // Send
  479.     private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
  480.         Thread^ threadSend = gcnew Thread(gcnew ThreadStart(this, &MainForm::Send));
  481.         threadSend->Start();
  482.     }
  483.     private: System::Void MainForm_Load(System::Object^  sender, System::EventArgs^  e)
  484.     {
  485.         host = msclr::interop::marshal_as<std::string>(textBox2->Text);
  486.         remotePort = Convert::ToInt32(textBox3->Text);
  487.         localPort = Convert::ToInt32(textBox4->Text);
  488.         Thread^ threadListening = gcnew Thread(gcnew ThreadStart(this, &MainForm::Listening));
  489.         threadListening->Start();
  490.     }
  491. };
  492. }
Advertisement
Add Comment
Please, Sign In to add comment