#pragma once
bool connected = false;
namespace testserveur {
using namespace System;
using namespace System::IO;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::Threading;
using namespace System::Drawing::Imaging;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
myThread=gcnew Thread(gcnew ThreadStart(this,&Form1::Serveur));
}
protected:
/// <summary>
/// Nettoyage des ressources utilisées.
/// </summary>
~Form1()
{
if (components)
{
if (myThread->IsAlive) myThread->Abort();
delete components;
}
}
private: System::Windows::Forms::TextBox ^TB_Port;
protected:
private: System::Windows::Forms::Button ^BT_Start;
private: System::Windows::Forms::Label ^LB_ONOFF;
private: System::Windows::Forms::RichTextBox ^RTB_Log;
private: System::Windows::Forms::ProgressBar ^PB_Load;
//pour le serveur :
private: Threading::Thread ^myThread;
//static car utilisé dans le thread
private: static TcpListener ^server;
private: static TcpClient ^client;
//stream pour lire/ecrire sur le socket
private: static NetworkStream ^stream;
// pour le lecteur binaire sur le socket (plus pratique)
private: static BinaryWriter ^socketbinwrite;
private: static BinaryReader ^socketbinread;
//binary Reader sur les fichiers
private: static BinaryReader ^FichierBinReader;
private: static BinaryWriter ^FichierBinWrite;
private:
/// <summary>
/// Variable nécessaire au concepteur.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
/// le contenu de cette méthode avec l'éditeur de code.
/// </summary>
void InitializeComponent(void)
{
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
this->TB_Port = (gcnew System::Windows::Forms::TextBox());
this->BT_Start = (gcnew System::Windows::Forms::Button());
this->LB_ONOFF = (gcnew System::Windows::Forms::Label());
this->RTB_Log = (gcnew System::Windows::Forms::RichTextBox());
this->PB_Load = (gcnew System::Windows::Forms::ProgressBar());
this->SuspendLayout();
//
// TB_Port
//
this->TB_Port->Location = System::Drawing::Point(12, 12);
this->TB_Port->Name = L"TB_Port";
this->TB_Port->Size = System::Drawing::Size(49, 20);
this->TB_Port->TabIndex = 0;
this->TB_Port->Text = L"8085";
//
// BT_Start
//
this->BT_Start->Location = System::Drawing::Point(67, 10);
this->BT_Start->Name = L"BT_Start";
this->BT_Start->Size = System::Drawing::Size(75, 23);
this->BT_Start->TabIndex = 1;
this->BT_Start->Text = L"Démarrer";
this->BT_Start->UseVisualStyleBackColor = true;
this->BT_Start->Click += gcnew System::EventHandler(this, &Form1::BT_Start_Click);
//
// LB_ONOFF
//
this->LB_ONOFF->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->LB_ONOFF->Location = System::Drawing::Point(220, 9);
this->LB_ONOFF->Name = L"LB_ONOFF";
this->LB_ONOFF->Size = System::Drawing::Size(52, 23);
this->LB_ONOFF->TabIndex = 2;
this->LB_ONOFF->Text = L"[OFF]";
this->LB_ONOFF->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
//
// RTB_Log
//
this->RTB_Log->Location = System::Drawing::Point(12, 40);
this->RTB_Log->Name = L"RTB_Log";
this->RTB_Log->Size = System::Drawing::Size(260, 260);
this->RTB_Log->TabIndex = 3;
this->RTB_Log->Text = L"";
//
// PB_Load
//
this->PB_Load->Location = System::Drawing::Point(12, 306);
this->PB_Load->Name = L"PB_Load";
this->PB_Load->Size = System::Drawing::Size(260, 14);
this->PB_Load->TabIndex = 4;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 332);
this->Controls->Add(this->PB_Load);
this->Controls->Add(this->RTB_Log);
this->Controls->Add(this->LB_ONOFF);
this->Controls->Add(this->BT_Start);
this->Controls->Add(this->TB_Port);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
this->Icon = (cli::safe_cast<System::Drawing::Icon^ >(resources->GetObject(L"$this.Icon" )));
this->Name = L"Form1";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = L"Serveur de Test";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void BT_Start_Click(System::Object^ sender, System::EventArgs^ e) {
if(!connected){
myThread->Start();
this->Text = "Thread Start";
this->BT_Start->Text = "Arrêter";
this->LB_ONOFF->Text = "[ON]";
}else{
myThread->Abort();
server->Stop();
this->Text = "Thread Stop";
this->BT_Start->Text = "Démarrer";
this->LB_ONOFF->Text = "[OFF]";
myThread->Sleep(500);
myThread->Abort();
}
this->TB_Port->Enabled = connected;
connected = !connected;
}
void Serveur()
{
String^message;
server = gcnew TcpListener(Convert::ToInt32( TB_Port->Text));
server->Start();
RTB_Log->AppendText("Serveur démaré, attente de client...\n" );
client = server->AcceptTcpClient();
stream = client->GetStream();
socketbinread = gcnew BinaryReader(stream);
socketbinwrite = gcnew BinaryWriter(stream);
RTB_Log->AppendText("client connecté\n" );
//boucle de reception des messages
while(message=socketbinread->ReadString())
{
// ------------------------
// # Commande
// ------------------------
}
}
};
}