Hello
No notes
Syntax:
C++
#pragma once namespace Server { 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::Threading; using namespace System::Net; using namespace System::Net::Sockets; using namespace System::IO; public ref struct wordSet { String^ text; bool isTyped; }; public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); } protected: ~Form1() { if (components) { delete components; } } private: System::Threading::Thread^ serverThread; private: System::Net::Sockets::Socket^ serverSocket; private: System::Windows::Forms::ListBox^ listBoxMsg; private: System::Windows::Forms::Button^ btnStart; private: System::Windows::Forms::Label^ lblMsg; private: System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code void InitializeComponent(void) { this->listBoxMsg = (gcnew System::Windows::Forms::ListBox()); this->btnStart = (gcnew System::Windows::Forms::Button()); this->lblMsg = (gcnew System::Windows::Forms::Label()); this->SuspendLayout(); // // listBoxMsg // this->listBoxMsg->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->listBoxMsg->FormattingEnabled = true; this->listBoxMsg->ItemHeight = 12; this->listBoxMsg->Location = System::Drawing::Point(12, 42); this->listBoxMsg->Name = L"listBoxMsg"; this->listBoxMsg->Size = System::Drawing::Size(260, 208); this->listBoxMsg->TabIndex = 0; // // btnStart // this->btnStart->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Right)); this->btnStart->Location = System::Drawing::Point(197, 12); this->btnStart->Name = L"btnStart"; this->btnStart->Size = System::Drawing::Size(75, 23); this->btnStart->TabIndex = 1; this->btnStart->Text = L"開始聆聽"; this->btnStart->UseVisualStyleBackColor = true; this->btnStart->Click += gcnew System::EventHandler(this, &Form1::btnStart_Click); // // lblMsg // this->lblMsg->AutoSize = true; this->lblMsg->Location = System::Drawing::Point(12, 17); this->lblMsg->Name = L"lblMsg"; this->lblMsg->Size = System::Drawing::Size(53, 12); this->lblMsg->TabIndex = 2; this->lblMsg->Text = L"歷史訊息"; // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 12); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(284, 262); this->Controls->Add(this->lblMsg); this->Controls->Add(this->btnStart); this->Controls->Add(this->listBoxMsg); this->Name = L"Form1"; this->Text = L"打字遊戲 (伺服端)"; this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &Form1::Form1_FormClosing); this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion private: delegate System::Void addEventDelegate(String^ s); private: System::Void addEvent(String^ s) { try { if (this->InvokeRequired) { addEventDelegate^ d = gcnew addEventDelegate(this, &Server::Form1::addEvent); this->Invoke(d, s); } else { this->listBoxMsg->Items->Insert(0, s); } } catch (Exception^ e) { this->listBoxMsg->Items->Insert(0, e->ToString()); } } private: System::String^ getRandomString() { String^ s = ""; Random^ r = gcnew Random((int)DateTime::Now.Ticks); // 產生一組五字元的String for (int i = 0; i < 5; i++) { // 決定大小寫 if (r->Next() % 2 == 0) { s += Char('a' + r->Next(26)).ToString(); } else { s += Char('A' + r->Next(26)).ToString(); } } System::Threading::Thread::Sleep(10); return s; } private: System::Void generateRandText(array<wordSet^>^ ws) { try { for (int i = 0; i < 5; i++) { ws[i]->text = this->getRandomString(); ws[i]->isTyped = false; } } catch (Exception^ e) { this->addEvent(e->ToString()); } } private: System::Void socketHandler(Object^ tmpSocket) { try { String^ recvMsg; NetworkStream^ conn = gcnew NetworkStream((Socket^) tmpSocket); BinaryReader^ reader = gcnew BinaryReader(conn); BinaryWriter^ writer = gcnew BinaryWriter(conn); do { recvMsg = reader->ReadString(); this->addEvent("收到訊息: " + recvMsg); array<wordSet^>^ ws = gcnew array<wordSet^>(5); generateRandText(ws); for (int i = 0; i < 5; i++) { writer->Write(ws[i]->text + Environment::NewLine); } // Wait System::Threading::Thread::Sleep(10); } while (((Socket^)tmpSocket)->Connected); } catch (Exception^ e) { this->addEvent("使用者已斷線, 或發生錯誤"); this->addEvent(e->ToString()); } } private: System::Void runServer() { TcpListener^ listener; try { listener = gcnew TcpListener(IPAddress::Parse("127.0.0.1"), 4500); listener->Start(); while (true) { this->serverSocket = listener->AcceptSocket(); this->addEvent("收到新的連線要求"); Thread^ newPlayer = gcnew Thread(gcnew ParameterizedThreadStart(this, &Server::Form1::socketHandler)); newPlayer->Start(this->serverSocket); } } catch (SocketException^ se) { this->addEvent(se->ToString()); } catch (Exception^ e) { this->addEvent(e->ToString()); } } private: System::Void Form1_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e) { System::Environment::Exit(System::Environment::ExitCode); } private: System::Void btnStart_Click(System::Object^ sender, System::EventArgs^ e) { this->serverThread = gcnew Thread(gcnew ThreadStart(this, &Form1::runServer)); this->serverThread->Start(); this->addEvent("開始監聽連線"); } }; }