📄 main.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "main.h"
#include "about.h"
#include <stdio.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox14Click(TObject *Sender)
{
Label1->Enabled=CheckBox14->Checked;
Edit2->Enabled=CheckBox14->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit2KeyPress(TObject *Sender, char &Key)
{
if ((Key!=8)&&((Key>'9')||(Key<'0'))) Key=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit5KeyPress(TObject *Sender, char &Key)
{ AnsiString asTxt;
if ((Key!=8)&&((Key>'9')||(Key<'0'))) Key=0;
else if(Key!=8)
{ asTxt=Edit5->Text+(AnsiString)Key;
try
{ if(asTxt.ToInt()>65535) Key=0;}
catch(...)
{Key=0;}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit4KeyPress(TObject *Sender, char &Key)
{
if ((Key<='f')&&(Key>='a')) Key=Key+'A'-'a';
if(!(((Key<='9')&&(Key>='0'))||((Key<='F')&&(Key>='A'))||(Key==8))) Key=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit4Change(TObject *Sender)
{
AnsiString asText=Edit4->Text;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox9Click(TObject *Sender)
{
CheckBox6->Enabled= CheckBox9->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox8Click(TObject *Sender)
{
CheckBox7->Enabled= CheckBox8->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
if(SpeedButton1->Down)
{
try
{Timer1->Interval=Edit2->Text.ToInt();}
catch(...)
{Timer1->Interval=1000;}
Timer1->Enabled=CheckBox14->Checked;
while(DataLock) Delay(5);
DataLock=true;
try
{
int nSendByte=BuiltUpBuf(CheckBox11->Checked,CheckBox12->Checked,Edit3->Text,Edit4->Text);
SendData(nSendByte );
}
catch(...) { }
DataLock=false;
if(!Timer1->Enabled) SpeedButton1->Down=false;
}
else Timer1->Enabled=SpeedButton1->Down;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox11Click(TObject *Sender)
{
Edit3->Enabled=CheckBox11->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox12Click(TObject *Sender)
{
Edit4->Enabled=CheckBox12->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox2Click(TObject *Sender)
{
Edit1->Enabled= CheckBox2->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox15Click(TObject *Sender)
{
Edit6->Enabled= CheckBox15->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
try
{ GSER->Port=Edit5->Text.ToInt();}
catch(...)
{GSER->Port=1024;}
GSER->Active=true;
Edit5->Enabled=false;
SpeedButton2->Enabled=false;
SpeedButton3->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton3Click(TObject *Sender)
{
Edit5->Enabled=true;
GSER->Active=false;
SpeedButton3->Enabled=false;
SpeedButton2->Enabled=true;
AddrList->Items->Clear();
Label3->Caption="连接用户列表:";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton5Click(TObject *Sender)
{
GSER->Active=false;
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
SpeedButton3->Enabled=false;
SpeedButton2->Enabled=true;
DataLock=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::GSERAccept(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->Panels->Items[0]->Text = "收到新连接: " + Socket->RemoteAddress;
Label3->Caption=Format("现有 %d 个连接", ARRAYOFCONST(( GSER->Socket->ActiveConnections)));
AnsiString asTmp=IntToStr(Socket->SocketHandle);
AddrList->Items->Add(Socket->RemoteAddress+"::"+asTmp);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::GSERClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->Panels->Items[0]->Text = "与 " + Socket->RemoteAddress+" 断开连接";
int iLinker=GSER->Socket->ActiveConnections-1;
if(iLinker>0)
Label3->Caption=Format("现有 %d 个连接", ARRAYOFCONST((iLinker)));
else Label3->Caption="连接用户列表:";
int hdSk;
AnsiString asTmp;
for(int i=0;i<AddrList->Count;i++)
{ hdSk=AddrList->Items->Strings[i].Pos("::")+2;
asTmp=AddrList->Items->Strings[i].SubString(hdSk,AddrList->Items->Strings[i].Length()-hdSk+1);
try{hdSk=asTmp.ToInt();} catch(...){hdSk=0;}
if(hdSk==Socket->SocketHandle) AddrList->Items->Delete(i);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::GSERClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{ AnsiString asTmp;
int nRecByte=Socket->ReceiveLength();
char *cRecByte=new char[nRecByte];
Socket->ReceiveBuf(cRecByte,nRecByte);
asTmp=" 从 "+Socket->RemoteAddress+" 接收到数据:";
DispCommData(cRecByte, nRecByte,asTmp);
SaveCommData();
if(CheckBox16->Checked) HeartRPT(cRecByte,nRecByte,Socket);
if(CheckBox13->Checked)
{
while(DataLock) Delay(5);
DataLock=true;
try
{ int nSendByte=BuiltUpBuf(CheckBox2->Checked,CheckBox15->Checked,Edit1->Text,Edit6->Text);
Socket->SendBuf(cOutDatBuf,nSendByte);
}
catch(...)
{ }
DataLock=false;
}
delete cRecByte;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TForm1::RecByteToTxt(unsigned char *cRecByte,int nRecByte)
{
unsigned char *pDst;
int iSpec=0;
unsigned ucTmp;
pDst= cRecByte;
for(int i=0; i<nRecByte; i++)
{ if((*pDst<32)||((*pDst>0x7F)&&(*pDst<0xA0))) iSpec++;
pDst++;
}
char *cDBuf=new char[nRecByte+iSpec*4+1];
const char tab[]="0123456789ABCDEF"; // 0x0-0xf的字符查找表
pDst=cDBuf;
for(int i=0; i<nRecByte; i++)
{ if((*cRecByte<32)||((*cRecByte>0x7F)&&(*cRecByte<0xA0)))
{ *pDst++='\\';*pDst++='X';
ucTmp=*cRecByte&0xff;
// 输出低4位
*pDst++ = tab[ucTmp >> 4];
// 输出高4位
*pDst++ = tab[ucTmp & 0x0f];
cRecByte++;
}
else *pDst++=*cRecByte++;
}
*pDst = '\0';
AnsiString asTmp=cDBuf;
delete cDBuf;
return asTmp;
}
AnsiString __fastcall TForm1::RecByteToHex(char *cRecByte,int nRecByte)
{
char *cDBuf=new char[nRecByte*3+1];
const char tab[]="0123456789ABCDEF"; // 0x0-0xf的字符查找表
unsigned ucTmp;
char *pDst;
pDst=cDBuf;
for(int i=0; i<nRecByte; i++)
{
// 输出低4位
ucTmp=*cRecByte&0xff;
*pDst++ = tab[ucTmp >> 4];
// 输出高4位
*pDst++ = tab[ucTmp & 0x0f];
*pDst++ =' ';
cRecByte++;
}
// 输出字符串加个结束符
*pDst = '\0';
AnsiString asTmp=cDBuf;
delete cDBuf;
return asTmp;
}
void __fastcall TForm1::SpeedButton4Click(TObject *Sender)
{
for(int i=0;i<GSER->Socket->ActiveConnections;i++)
{ GSER->Socket->Connections[i]->Close();
}
GSER->Active=false;
AddrList->Items->Clear();
Label3->Caption="连接用户列表:";
StatusBar1->Panels->Items[0]->Text = "断开所有连接";
GSER->Active=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton6Click(TObject *Sender)
{ int hdSk;
AnsiString asTmp;
for(int i=0;i<AddrList->Count;i++)
{ try
{
if(AddrList->Checked[i])
{
hdSk=AddrList->Items->Strings[i].Pos("::")+2;
asTmp=AddrList->Items->Strings[i].SubString(hdSk,AddrList->Items->Strings[i].Length()-hdSk+1);
try{hdSk=asTmp.ToInt();} catch(...){hdSk=0;}
for(int j=0;j<GSER->Socket->ActiveConnections;j++)
if(hdSk==GSER->Socket->Connections[j]->SocketHandle) GSER->Socket->Connections[i]->Close();
}
}
catch(...)
{ }
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -