C++ Builder NetMaster中的NMEcho控件来实现网络连接检测功能??咋不行呀
首先定义以下控件:
三个Edit控件:
一个用于接收远程主机的IP地址或域名,
一个用于接收用户设置的超时机制的时间,
一个用于设置端口号。
两个RichEdit控件:
一个用于给远程主机发送信息,
一个用于接收来自远程主机的信息。
两个CheckBox控件:用于用户是否自己设定端口号。
一个Button控件:用于执行测试。
一个StatusBar控件:用于显示应用程序的状态。
程序实现代码如下:
void __fastcall TForm1::Button1Click(Tobject Sender)
{ //设置NMEcho控件的标准TCP/IP属性
NMEcho1->Host=Edit1->Text ;
NMEcho1->TimeOut=StrToInt(Edit2->Text) ;
if(CheckBox1->Checked)
NMEcho1->Port=StrToInt(Edit3->Text);
else
NMEcho1->Port=7;
//TCP/IP中Echo的默认端口号
NMEcho1->ReportLevel=Status_Basic;
NMEcho1->Connect(); //建立连接
RichEdit2->Clear ();
for(int i=0;iLines->Count;i++)
//RichEdit1用于给远程主机发送信息
RichEdit2->Text=RichEdit2->Text +NMEcho1->Echo(RichEdit1->Lines-> Strings[i]);
NMEcho1->Disconnect ();
}
注意:在调用NMEcho控件的Connect()方法时,应该确保在接收数据之前连接已经建立。
当调用Connect()方法后,如果用户输入的是域地址而不是IP地址,且域名服务器成功地解析了这个域名,将触发控件的OnHostResoved事件,在此事件的处理中,我们将解析成功的消息在状态栏中显示给用户。
具体实现代码如下:
void __fastcall TForm1::NMEcho1HostResolved(Tcomponent Sender)
{
StatusBar1->Panels->Items[0]->Text="Host Resolved!";
}
如果用户输入的远程主机不正确,将触发控件的OnInvalidHost事件,在此事件的处理中,弹出对话框要求用户重新输入远程主机的IP地址或域名地址,然后试图与服务器重建连接。
具体代码如下:
void __fastcall TForm1::NMEcho1InvalidHost(bool &&Handled)
{
AnsiString s;
if(InputQuery("Invailid host!","Specify a new host:",s))
{
NMEcho1->Host=s;
Handled=true;
}
}
建立连接后,将触发控件的OnConnect事件,在此事件的处理中,我们将连接成功的消息在状态栏中显示给用户。
具体实现代码如下:
void __fastcall TForm1::NMEcho1Connect(Tobject Sender)
{
StatusBar1->Panels->Items[0]->Text="Echo has connected host!";
}
如果在调用Connect()方法后,在超时时间仍然没有与服务器连接,将触发控件的OnConnectFailed事件,在此事件的处理中,我们将连接失败的消息显示给用户。
具体实现代码如下:
void __fastcall TForm1::NMEcho1ConnectionFailed(Tobject Sender)
{ ShowMessage("Connection failed!");
}
除了NMEcho控件可以实现以上功能外,NetMaster的NMDayTime、NMTime这两个控件也能实现。
方法与NMEcho控件一样,区别是NMDayTime和NMTime这两个控件不用首先调用Connect()方法,
它们与服务器的连接是在使用DayTimeStr、TimeStr属性时自动进行的。
-------------------------------------
-------------------------------------
我是照上边写的
-----------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
NMEcho1->Host=Edit1->Text;
NMEcho1->TimeOut=StrToInt(Edit2->Text);
if(CheckBox1->Enabled)
{
NMEcho1->Port=StrToInt(Edit3->Text);
}
else
{
NMEcho1->Port=7;
}
NMEcho1->ReportLevel=Status_Basic;
NMEcho1->Connect(); //建立连接
RichEdit2->Clear();
for(int i=0;i<RichEdit1->Lines->Count;i++)
//RichEdit1用于给远程主机发送信息
RichEdit2->Text=RichEdit2->Text+NMEcho1->Echo(RichEdit1->Lines->Strings[0]);
NMEcho1->Disconnect();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMEcho1HostResolved(TComponent *Sender)
{
StatusBar1->Panels->Items[0]->Text="Host Resolved!";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMEcho1InvalidHost(bool &Handled)
{
AnsiString s;
if(InputQuery("Invailid host!","Specify a new host:",s))
{
NMEcho1->Host=s;
Handled=true;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMEcho1Connect(TObject *Sender)
{
StatusBar1->Panels->Items[0]->Text="Echo has connected host!";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMEcho1ConnectionFailed(TObject *Sender)
{
ShowMessage("不能建立连接!");
}
//---------------------------------------------------------------------------
总是提示 ”不能建立连接“
不知是这文章不对还是俺错了