📄 unit1.~cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int g_ConnectState=0;//连接状态
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void TForm1::SetConnectBtnState()
{
//TODO: Add your source code here
switch(g_ConnectState)
{
case 0:
Button1->Caption="连接";
Button1->Enabled=TRUE;
break;
case 1:
Button1->Caption="断开";
Button1->Enabled=TRUE;
break;
case 2:
Button1->Enabled=FALSE;
break;
}
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(g_ConnectState==0)
{
ClientSocket1->Address=Edit1->Text;
ClientSocket1->Port=atoi(Edit2->Text.c_str());
ClientSocket1->Open();
g_ConnectState=2;
SetConnectBtnState();
}
else
{
ClientSocket1->Close();
g_ConnectState=0;
SetConnectBtnState();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
g_ConnectState=1;
SetConnectBtnState();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnDisConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
g_ConnectState=0;
ClientSocket1->Close();
SetConnectBtnState();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnRead(TObject *Sender, TCustomWinSocket *Socket)
{
Edit4->Text+=ClientSocket1->Socket->ReceiveText();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ClientSocket1->Socket->SendText(Edit3->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnError(TObject *Sender, TCustomWinSocket *Socket,
TErrorEvent ErrorEvent, int &ErrorCode)
{
g_ConnectState=0;
ClientSocket1->Close();
SetConnectBtnState();
ErrorCode=0;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -