Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //MainForm.cpp
- #include "MainForm.h"
- using namespace System;
- using namespace System::Windows::Forms;
- [STAThreadAttribute]
- void Main(array<String^>^ args) {
- Application::EnableVisualStyles();
- Application::SetCompatibleTextRenderingDefault(false);
- WinForms_CN::MainForm form;
- Application::Run(%form);
- }
- //MainForm.h
- #pragma once
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include <iostream>
- #include <string>
- #include <msclr\marshal_cppstd.h>
- #pragma comment (lib, "Ws2_32.lib")
- namespace WinForms_CN {
- using namespace System;
- using namespace System::ComponentModel;
- using namespace System::Collections;
- using namespace System::Windows::Forms;
- using namespace System::Data;
- using namespace System::Drawing;
- using namespace System::Runtime::InteropServices;
- using namespace System::Threading;
- public struct client_type
- {
- SOCKET socket;
- int id;
- char received_message[512];
- };
- public struct client_type_connect
- {
- SOCKET socket;
- int id;
- };
- struct addrinfo *result = NULL, *ptr = NULL, hints;
- std::string host;
- int remotePort;
- int localPort;
- const char OPTION_VALUE = 1;
- const int MAX_CLIENTS = 5;
- std::string old = "";
- std::vector<client_type_connect> client_array(MAX_CLIENTS);
- client_type_connect &new_client = client_array[0];
- public ref class MainForm : public System::Windows::Forms::Form
- {
- public:
- MainForm(void)
- {
- InitializeComponent();
- }
- protected:
- ~MainForm()
- {
- if (components)
- {
- delete components;
- }
- }
- private: System::Windows::Forms::Button^ button1;
- private: System::Windows::Forms::TextBox^ textBox1;
- private: System::Windows::Forms::Button^ button2;
- private: System::Windows::Forms::TextBox^ textBox2;
- private: System::Windows::Forms::TextBox^ textBox3;
- private: System::Windows::Forms::TextBox^ textBox4;
- private: System::Windows::Forms::Label^ label1;
- private: System::Windows::Forms::Label^ label2;
- private: System::Windows::Forms::Label^ label3;
- private: System::Windows::Forms::TextBox^ textBox5;
- private: System::Windows::Forms::Button^ button3;
- protected:
- private:
- System::ComponentModel::Container ^components;
- delegate void UpdateTextBox1delegate(System::String^ someText);
- void Update(System::String^ someText) {
- if (this->textBox1->InvokeRequired) {
- UpdateTextBox1delegate^ d = gcnew UpdateTextBox1delegate(this, &MainForm::Update);
- this->Invoke(d, gcnew array<Object^> { someText });
- }
- else {
- this->textBox1->Text += someText + "\r\n";
- }
- }
- #pragma region Windows Form Designer generated code
- void InitializeComponent(void)
- {
- this->button1 = (gcnew System::Windows::Forms::Button());
- this->textBox1 = (gcnew System::Windows::Forms::TextBox());
- this->button2 = (gcnew System::Windows::Forms::Button());
- this->textBox2 = (gcnew System::Windows::Forms::TextBox());
- this->textBox3 = (gcnew System::Windows::Forms::TextBox());
- this->textBox4 = (gcnew System::Windows::Forms::TextBox());
- this->label1 = (gcnew System::Windows::Forms::Label());
- this->label2 = (gcnew System::Windows::Forms::Label());
- this->label3 = (gcnew System::Windows::Forms::Label());
- this->textBox5 = (gcnew System::Windows::Forms::TextBox());
- this->button3 = (gcnew System::Windows::Forms::Button());
- this->SuspendLayout();
- //
- // button1
- //
- this->button1->Location = System::Drawing::Point(404, 45);
- this->button1->Name = L"button1";
- this->button1->Size = System::Drawing::Size(70, 23);
- this->button1->TabIndex = 0;
- this->button1->Text = L"Change";
- this->button1->UseVisualStyleBackColor = true;
- this->button1->Click += gcnew System::EventHandler(this, &MainForm::button1_Click);
- //
- // textBox1
- //
- this->textBox1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
- | System::Windows::Forms::AnchorStyles::Left)
- | System::Windows::Forms::AnchorStyles::Right));
- this->textBox1->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
- static_cast<System::Byte>(204)));
- this->textBox1->Location = System::Drawing::Point(12, 97);
- this->textBox1->Multiline = true;
- this->textBox1->Name = L"textBox1";
- this->textBox1->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
- this->textBox1->Size = System::Drawing::Size(732, 283);
- this->textBox1->TabIndex = 1;
- //
- // button2
- //
- this->button2->Location = System::Drawing::Point(480, 45);
- this->button2->Name = L"button2";
- this->button2->Size = System::Drawing::Size(70, 23);
- this->button2->TabIndex = 2;
- this->button2->Text = L"Clear";
- this->button2->UseVisualStyleBackColor = true;
- this->button2->Click += gcnew System::EventHandler(this, &MainForm::button2_Click);
- //
- // textBox2
- //
- this->textBox2->Location = System::Drawing::Point(118, 9);
- this->textBox2->Name = L"textBox2";
- this->textBox2->Size = System::Drawing::Size(154, 20);
- this->textBox2->TabIndex = 3;
- this->textBox2->Text = L"localhost";
- //
- // textBox3
- //
- this->textBox3->Location = System::Drawing::Point(148, 45);
- this->textBox3->Name = L"textBox3";
- this->textBox3->Size = System::Drawing::Size(124, 20);
- this->textBox3->TabIndex = 4;
- this->textBox3->Text = L"5000";
- //
- // textBox4
- //
- this->textBox4->Location = System::Drawing::Point(400, 9);
- this->textBox4->Name = L"textBox4";
- this->textBox4->Size = System::Drawing::Size(150, 20);
- this->textBox4->TabIndex = 5;
- this->textBox4->Text = L"5000";
- //
- // label1
- //
- this->label1->AutoSize = true;
- this->label1->Location = System::Drawing::Point(12, 12);
- this->label1->Name = L"label1";
- this->label1->Size = System::Drawing::Size(100, 13);
- this->label1->TabIndex = 6;
- this->label1->Text = L"Remote node name";
- //
- // label2
- //
- this->label2->AutoSize = true;
- this->label2->Location = System::Drawing::Point(12, 48);
- this->label2->Name = L"label2";
- this->label2->Size = System::Drawing::Size(130, 13);
- this->label2->TabIndex = 7;
- this->label2->Text = L"Remote node port number";
- //
- // label3
- //
- this->label3->AutoSize = true;
- this->label3->Location = System::Drawing::Point(302, 12);
- this->label3->Name = L"label3";
- this->label3->Size = System::Drawing::Size(92, 13);
- this->label3->TabIndex = 8;
- this->label3->Text = L"Local port number";
- //
- // textBox5
- //
- this->textBox5->Anchor = static_cast<System::Windows::Forms::AnchorStyles>(((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left)
- | System::Windows::Forms::AnchorStyles::Right));
- this->textBox5->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
- static_cast<System::Byte>(204)));
- this->textBox5->Location = System::Drawing::Point(12, 386);
- this->textBox5->Multiline = true;
- this->textBox5->Name = L"textBox5";
- this->textBox5->Size = System::Drawing::Size(667, 66);
- this->textBox5->TabIndex = 9;
- //
- // button3
- //
- this->button3->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
- this->button3->Location = System::Drawing::Point(682, 386);
- this->button3->Name = L"button3";
- this->button3->Size = System::Drawing::Size(62, 66);
- this->button3->TabIndex = 10;
- this->button3->Text = L"Send";
- this->button3->UseVisualStyleBackColor = true;
- this->button3->Click += gcnew System::EventHandler(this, &MainForm::button3_Click);
- //
- // MainForm
- //
- this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
- this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
- this->ClientSize = System::Drawing::Size(755, 459);
- this->Controls->Add(this->button3);
- this->Controls->Add(this->textBox5);
- this->Controls->Add(this->label3);
- this->Controls->Add(this->label2);
- this->Controls->Add(this->label1);
- this->Controls->Add(this->textBox4);
- this->Controls->Add(this->textBox3);
- this->Controls->Add(this->textBox2);
- this->Controls->Add(this->button2);
- this->Controls->Add(this->textBox1);
- this->Controls->Add(this->button1);
- this->Name = L"MainForm";
- this->Text = L"Lab_7 CN";
- this->Load += gcnew System::EventHandler(this, &MainForm::MainForm_Load);
- this->ResumeLayout(false);
- this->PerformLayout();
- }
- #pragma endregion
- void Send() {
- WSADATA wsa_data;
- std::string sent_message = "";
- client_type client = { INVALID_SOCKET, -1, "" };
- int iResult = 0;
- std::string message;
- // Initialize Winsock
- iResult = WSAStartup(MAKEWORD(2, 2), &wsa_data);
- ZeroMemory(&hints, sizeof(hints));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- // Resolve the server address and port
- std::string tmp = std::to_string(localPort);
- iResult = getaddrinfo(host.c_str(), tmp.c_str(), &hints, &result);
- if (iResult != 0) {
- WSACleanup();
- }
- for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
- client.socket = socket(ptr->ai_family, ptr->ai_socktype,
- ptr->ai_protocol);
- if (client.socket == INVALID_SOCKET) {
- WSACleanup();
- }
- iResult = connect(client.socket, ptr->ai_addr, (int)ptr->ai_addrlen);
- if (iResult == SOCKET_ERROR) {
- closesocket(client.socket);
- client.socket = INVALID_SOCKET;
- continue;
- }
- break;
- }
- freeaddrinfo(result);
- if (client.socket == INVALID_SOCKET) {
- WSACleanup();
- }
- recv(client.socket, client.received_message, 512, 0);
- message = client.received_message;
- if (message != "Server is full")
- {
- client.id = atoi(client.received_message);
- sent_message = msclr::interop::marshal_as<std::string>(textBox5->Text);
- new_client.socket = client.socket;
- iResult = send(client.socket, sent_message.c_str(), strlen(sent_message.c_str()), 0);
- /*std::string tttmp = "Send to socket #" + std::to_string(client.socket) + ": " + sent_message;
- MessageBox::Show(gcnew String(tttmp.c_str()));*/
- }
- iResult = shutdown(client.socket, SD_SEND);
- if (iResult == SOCKET_ERROR) {
- closesocket(client.socket);
- WSACleanup();
- }
- closesocket(client.socket);
- WSACleanup();
- }
- void process_client_connect()
- {
- std::string msg = "";
- old = "";
- char tempmsg[512] = "";
- String^ mes = "***Connection established***\r\n"
- + "Remote host: "
- + gcnew String(host.c_str()) + "\n";
- this->Invoke(gcnew UpdateTextBox1delegate(this, &MainForm::Update), gcnew array<Object^> { mes });
- memset(tempmsg, 0, 512);
- int iResult = recv(new_client.socket, tempmsg, 512, 0);
- strcmp("", tempmsg);
- msg = "SOCKET #" + std::to_string(new_client.socket) + ": " + tempmsg;
- std::cout << msg.c_str() << std::endl;
- String^ message = gcnew String(msg.c_str());
- this->Invoke(gcnew UpdateTextBox1delegate(this, &MainForm::Update), gcnew array<Object^> { message });
- for (int i = 0; i < MAX_CLIENTS; i++)
- {
- if (client_array[i].socket != INVALID_SOCKET)
- if (new_client.id != i)
- iResult = send(client_array[i].socket, msg.c_str(), strlen(msg.c_str()), 0);
- }
- msg = "Client #" + std::to_string(new_client.id) + " Disconnected";
- mes = "***Connection broken***\r\n\r\n";
- this->Invoke(gcnew UpdateTextBox1delegate(this, &MainForm::Update), gcnew array<Object^> { mes });
- closesocket(new_client.socket);
- closesocket(client_array[new_client.id].socket);
- client_array[new_client.id].socket = INVALID_SOCKET;
- for (int i = 0; i < MAX_CLIENTS; i++)
- {
- if (client_array[i].socket != INVALID_SOCKET)
- iResult = send(client_array[i].socket, msg.c_str(), strlen(msg.c_str()), 0);
- }
- }
- void Listening() {
- host = msclr::interop::marshal_as<std::string>(textBox2->Text);
- remotePort = Convert::ToInt32(textBox3->Text);
- localPort = Convert::ToInt32(textBox4->Text);
- //host = "localhost";
- //localPort = 5000;
- WSADATA wsaData;
- struct addrinfo hints;
- struct addrinfo *server = NULL;
- SOCKET server_socket = INVALID_SOCKET;
- std::string msg = "";
- std::vector<client_type_connect> client(MAX_CLIENTS);
- int num_clients = 0;
- int temp_id = -1;
- WSAStartup(MAKEWORD(2, 2), &wsaData);
- ZeroMemory(&hints, sizeof(hints));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- hints.ai_flags = AI_PASSIVE;
- std::string tmp = std::to_string(localPort);
- getaddrinfo(host.c_str(), tmp.c_str(), &hints, &server);
- server_socket = socket(server->ai_family, server->ai_socktype, server->ai_protocol);
- setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &OPTION_VALUE, sizeof(int));
- setsockopt(server_socket, IPPROTO_TCP, TCP_NODELAY, &OPTION_VALUE, sizeof(int));
- bind(server_socket, server->ai_addr, (int)server->ai_addrlen);
- MessageBox::Show("Listening...");
- listen(server_socket, SOMAXCONN);
- for (int i = 0; i < MAX_CLIENTS; i++)
- {
- client[i].socket = -1;
- client[i].id = INVALID_SOCKET;
- }
- while (1)
- {
- SOCKET incoming = INVALID_SOCKET;
- incoming = accept(server_socket, NULL, NULL);
- if (incoming == INVALID_SOCKET) continue;
- num_clients = -1;
- temp_id = -1;
- for (int i = 0; i < MAX_CLIENTS; i++)
- {
- if (client[i].socket == INVALID_SOCKET && temp_id == -1)
- {
- client[i].socket = incoming;
- client[i].id = i;
- temp_id = i;
- }
- if (client[i].socket != INVALID_SOCKET)
- num_clients++;
- }
- if (temp_id != -1)
- {
- msg = std::to_string(client[temp_id].id);
- send(client[temp_id].socket, msg.c_str(), strlen(msg.c_str()), 0);
- new_client = client[temp_id];
- client_array = client;
- Thread^ threadProcess = gcnew Thread(gcnew ThreadStart(this, &MainForm::process_client_connect));
- threadProcess->Start();
- }
- else
- {
- msg = "Server is full";
- send(incoming, msg.c_str(), strlen(msg.c_str()), 0);
- }
- }
- closesocket(server_socket);
- for (int i = 0; i < MAX_CLIENTS; i++)
- closesocket(client[i].socket);
- WSACleanup();
- }
- // Change
- private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
- host = msclr::interop::marshal_as<std::string>(textBox2->Text);
- remotePort = Convert::ToInt32(textBox3->Text);
- localPort = Convert::ToInt32(textBox4->Text);
- }
- // Clear
- private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
- textBox1->Text = "";
- }
- // Send
- private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
- Thread^ threadSend = gcnew Thread(gcnew ThreadStart(this, &MainForm::Send));
- threadSend->Start();
- }
- private: System::Void MainForm_Load(System::Object^ sender, System::EventArgs^ e)
- {
- host = msclr::interop::marshal_as<std::string>(textBox2->Text);
- remotePort = Convert::ToInt32(textBox3->Text);
- localPort = Convert::ToInt32(textBox4->Text);
- Thread^ threadListening = gcnew Thread(gcnew ThreadStart(this, &MainForm::Listening));
- threadListening->Start();
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment