⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_main.cpp

📁 一个完成端口的框架程序
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "Test_Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{

}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
   char BindAddress[16];
   MyMsgID=RegisterWindowMessage("DumpMessage");
   IOCPServer.SetServicePort(9999);//设置端口
   //IOCPServer.SetBindAddress("127.0.0.1");//绑定地址
   IOCPServer.GetBindAddress(BindAddress,16);
   //IOCPServer.DropConnection()//断开一个连接
   //IOCPServer.DumpThread()
   IOCPServer.IsEchoServer=true;
   //IOCPServer.SetExpectThread(64);
   IOCPServer.SetRunningState(LS_WORK);
   //IOCPServer.SendText("Message");//发送段消息
//ShowMessage("sizeof(TtcpIOCP)="+IntToStr(sizeof(TtcpIOCP)));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WndProc(TMessage &Message)
{
   if(Message.Msg==MyMsgID)
   {
      char Msg[256];
      int iLen=(int)Message.LParam;
      memcpy(Msg,(char *)Message.WParam,iLen);
      Msg[iLen]='\0';
      Memo1->Lines->Add(Msg);
   }
   else
      TWinControl::WndProc(Message);
}
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{

   StaticText1->Caption=IntToStr(IOCPServer.Connections);
   StaticText2->Caption=IntToStr(IOCPServer.RecvSpeedInBytes);
   StaticText3->Caption=IntToStr(IOCPServer.SentSpeedInBytes);
   StaticText4->Caption=IntToStr(IOCPServer.ValidConnections);
   StaticText5->Caption=IntToStr(IOCPServer.ExpectThreadCount);
   StaticText6->Caption=IntToStr(IOCPServer.MaxConnections);
   StaticText7->Caption=IntToStr(IOCPServer.ThreadCount);      
}
//---------------------------------------------------------------------------




void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(SaveDialog1->Execute())
{
   Memo1->Lines->SaveToFile(SaveDialog1->FileName);
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
SIZE_T Length=HeapCompact(GetProcessHeap(),0);
if(Length==0)
Memo1->Lines->Add(Now().FormatString("yyyy-mm-dd hh:mm:ss ")+"内存整理失败("+IntToStr(GetLastError())+")!");
else
Memo1->Lines->Add(Now().FormatString("yyyy-mm-dd hh:mm:ss ")+"内存整理获得("+IntToStr(Length)+")Bytes空闲空间!");
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -