📄 client.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Client.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(Button1->Caption=="连接")
{
if(Edit1->Text!="")
{
Client->Address=Edit1->Text;
//将服务器IP地址赋值给客户端Client控件的Address属性
Client->Active=true;
//打开与服务器的Socket连接
}
}
else
Client->Active=false;
//将与服务器的Socket连接断开
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
AnsiString str1=Socket->ReceiveText();
// 用可变长度的AnsiString类字符串接受对方机发过来的信息
Memo1->Lines->Add(str1);
//将对方机发送来的信息通过Memo1控件显示
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
Memo1->Lines->Add("与对方连接已经断开");
Button1->Caption="连接";
//Button1按钮由名字"断开"更名为"连接"准备下一次连接
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
Memo1->Lines->Add("本机已经和"+Edit1->Text+"的主机建立连接。");
Button1->Caption="断开";
//将Button1由名称"连接"更名为"断开"
Edit1->Clear();
//IP地址文本框清空
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(Memo2->Lines->Strings[0]!="")
{
AnsiString str1="客户发送的信息:" +Memo2->Lines->Strings[0];
//用可变长度的AnsiString类字符串获取本机将要发送出去的数据
Memo2->Clear();
//发送信息文本区Memo1清空
Client->Socket->SendText(str1);
//本机发送信息
Memo1->Lines->Add(str1);
//将本机发送的信息在接受信息文本区Memo1显示
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Form1->Close();
//窗口调用Close()方法关闭
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
this->OpenDialog1->Filter="Text files(*.txt)|*.txt|All files(*.*)|*.*";
if(this->OpenDialog1->Execute())
{
this->Edit2->Text=this->OpenDialog1->FileName;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
if(this->Edit2->Text.Length()<1)
return;
if(this->Edit1->Text.Length()<1)
return;
TFileStream *MyFStream;
MyFStream=new TFileStream(this->Edit2->Text,fmOpenRead);
try
{
this->NMStrm1->Host=this->Edit1->Text;
this->NMStrm1->FromName="NetMasters";
this->NMStrm1->PostIt(MyFStream);
MessageBox(Handle,"发送文件到目标主机操作成功!","信息提示",MB_OK);
}
catch(...)
{
MessageBox(Handle,"发送文件到目标主机操作失败!","信息提示",MB_OK);
}
MyFStream->Free();
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -