C++ Biulder 案例展示:主机Host支持域名,不支持IP地址。
DNS服务器:可以把域名解析为IP地址。
C++ Biulder 安装包里的案例文件:D:\CBuilder5\Examples\FastNet\Echo
编译前后文件对比:Echodemo编译前7个文件,编译后增加了8个文件,包含*.exe应用程序。
界面文件Form 如下:文本显示、按键 Button、编辑框
注意:这里的Host需要输入域名,不支持IP地址。
NMEcho控件的7个事件:
1)OnConnect 连接事件
2)OnConnectionFailed 连接失败事件
3)OnConnectionRequired 连接请求事件
4)OnDisconnect 取消连接事件
5)OnHostResolved 解析主机(域名--->IP)
6)OnInvalidHost 主机名无效
7)OnStatus 状态
例程代码:
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "echomain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::NMEcho1Connect(TObject *Sender)
{
StatusBar1->SimpleText = "Connected";
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::NMEcho1ConnectionFailed(TObject *Sender)
{
ShowMessage("Connection Failed");
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::NMEcho1ConnectionRequired(bool &handled)
{
AnsiString BoxCaption;
AnsiString BoxMsg;
BoxCaption = "Connection Required";
BoxMsg = "Connection Required. Connect?";
if (MessageBox(0, &BoxMsg[1], &BoxCaption[1], MB_YESNO + MB_ICONEXCLAMATION) == IDYES)
{
handled = true;
Button2Click(this);
}
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Button2Click(TObject *Sender)
{
NMEcho1->ReportLevel = Status_Basic;
NMEcho1->TimeOut = 20000;
NMEcho1->Host = Edit2->Text;
NMEcho1->Port = StrToInt(Edit3->Text);
NMEcho1->Connect();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Button3Click(TObject *Sender)
{
NMEcho1->Disconnect();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Button1Click(TObject *Sender)
{
Edit4->Text = NMEcho1->Echo(Edit1->Text);
Label2->Caption = "Elapsed Time: "+FloatToStr(NMEcho1->ElapsedTime)+" milliseconds";
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Button4Click(TObject *Sender)
{
NMEcho1->Abort();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::NMEcho1Disconnect(TObject *Sender)
{
if (StatusBar1 != 0)
StatusBar1->SimpleText = "Disconnected";
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::NMEcho1HostResolved(TComponent *Sender)
{
StatusBar1->SimpleText = "Host resolved";
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::NMEcho1InvalidHost(bool &handled)
{
AnsiString NewHost;
if (InputQuery("Invalid Host", "Please Choose another host", NewHost))
{
NMEcho1->Host = NewHost;
handled = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::NMEcho1Status(TComponent *Sender,
AnsiString Status)
{
if (StatusBar1 != 0)
StatusBar1->SimpleText = Status;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "echomain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::NMEcho1Connect(TObject *Sender)
{
StatusBar1->SimpleText = "Connected";
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::NMEcho1ConnectionFailed(TObject *Sender)
{
ShowMessage("Connection Failed");
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::NMEcho1ConnectionRequired(bool &handled)
{
AnsiString BoxCaption;
AnsiString BoxMsg;
BoxCaption = "Connection Required";
BoxMsg = "Connection Required. Connect?";
if (MessageBox(0, &BoxMsg[1], &BoxCaption[1], MB_YESNO + MB_ICONEXCLAMATION) == IDYES)
{
handled = true;
Button2Click(this);
}
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Button2Click(TObject *Sender)
{
NMEcho1->ReportLevel = Status_Basic;
NMEcho1->TimeOut = 20000;
NMEcho1->Host = Edit2->Text;
NMEcho1->Port = StrToInt(Edit3->Text);
NMEcho1->Connect();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Button3Click(TObject *Sender)
{
NMEcho1->Disconnect();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Button1Click(TObject *Sender)
{
Edit4->Text = NMEcho1->Echo(Edit1->Text);
Label2->Caption = "Elapsed Time: "+FloatToStr(NMEcho1->ElapsedTime)+" milliseconds";
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Button4Click(TObject *Sender)
{
NMEcho1->Abort();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::NMEcho1Disconnect(TObject *Sender)
{
if (StatusBar1 != 0)
StatusBar1->SimpleText = "Disconnected";
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::NMEcho1HostResolved(TComponent *Sender)
{
StatusBar1->SimpleText = "Host resolved";
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::NMEcho1InvalidHost(bool &handled)
{
AnsiString NewHost;
if (InputQuery("Invalid Host", "Please Choose another host", NewHost))
{
NMEcho1->Host = NewHost;
handled = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::NMEcho1Status(TComponent *Sender,
AnsiString Status)
{
if (StatusBar1 != 0)
StatusBar1->SimpleText = Status;
}
//---------------------------------------------------------------------------