拜年!我的机子用的是宽带网,如何判断它是否连上网? (100分)

D

dok001

Unregistered / Unconfirmed
GUEST, unregistred user!
先向各位拜年!我的机子用的是宽带网(ADSL),如何判断它是否连上网?我试了坛子上好几个判断联网的方法,结果都是我拔了电话线,仍然显示连通.是不是与我的宽带网有关.
其实,我只是为取得网上服务器时间(已做到),若未连上网就不必去获取时间,不然会死机.
 
ping 服务器
 
ping 是控件还是函数,请详加指点!
 
晕倒!!!!!!!!!!!!!!!!!!!
 
下载一个上网计时软件吧,www.newhua.com
 
哦,原来你要编程实现
 
我想用编程的方法实现!
 
在GOOGLE自己搜"DELPHI+PING+控件"
 
我用的是delphi7,难道这么简单的问题,delphi7自带的控件实现不了,非要用第三方控件不行.
 
也可以用随便一个网格组件, 访问一下网络, 如果不行, 取回错误代码
 
to kkyy:
网格组件不会用,能细讲一下么?有没有范例?
 
ping 是一个dos命令,你可以在程序里调用也行,我记得好多地方都有在程序中调用ping
的例子,我现在的机器上没有这方面的东东,刚才去找了一个,应该有用,没用再说^&^!
hehe, 顺便问一句,你没学过dos吗?

Delphi编程实现Ping操作
  张泰立

  使用过网络的用户都熟悉“Ping”这个指令,它是一个DOS下的可执行文件,一般用它来检查网络连接的好坏程度。其基本原理是利用TCP/IP协议包中ICMP协议中的一个功能,即向所指定的计算机发送一个请求,收到请求的计算机返回一个应答,借此来判断该计算机是否在网上运行或者检查网络连接是否稳定可靠。在Ping程序执行过程中,双方计算机所耗费的资源都很少,因此,它是一个非常实用的工具。

  我们可以通过编程来实现“Ping”操作,对其加以改进,使之具有Windows的界面风格,显示比DOS更加直观。

  首先,对编程中需要的动态链接库作一简要说明:在Windows的System目录下,你可以找到Icmp.dll文件,该动态链接库提供了ICMP协议的所有功能,我们的编程就建立在对该动态链接库的调用上。

  Icmp.dll文件内的调用函数说明如下:

  1、IcmpCreateFile

  打开一个句柄,通过该句柄你可以发送ICMP的请求回送报文。

  2、IcmpCloseHandle

  关闭你通过IcmpCreateFile函数打开的句柄。

  3、IcmpSendEcho

  通过你打开的句柄发送ICMP请求,在超时或应答报文接收后返回。其参数基本上和它的帧结构一致,可参看下面的程序部分,其具体含意你可以参看有关ICMP协议的书籍。

  初步了解了上述的三个函数后,我们就可以开始编程了。

  首先,我们的程序运行后应该有如图1所示的基本功能。为此,我们可先在Delphi的窗口中放入右上图中所示的控件,如按钮、编辑框和文本显示框等。

  (G72.JPG)

  例程运行示意图

  然后,在程序的开始部分(FormCreate)对WinSocket进行初始化,其作用是申明使用的版本信息,同时调入Icmp.dll库。

  type

   PIPOptionInformation = ^TIPOptionInformation;

   TIPOptionInformation = packed record

   TTL: Byte;

   TOS: Byte;

   Flags: Byte;

   OptionsSize: Byte;

   OptionsData: PChar;

   end;

   PIcmpEchoReply = ^TIcmpEchoReply;

   TIcmpEchoReply = packed record

   Address: DWORD;

   Status: DWORD;

   RTT: DWORD;

   DataSize: Word;

   Reserved: Word;

   Data: Pointer;

   Options: TIPOptionInformation;

   end;

   TIcmpCreateFile = function: THandle; stdcall;

   TIcmpCloseHandle = function(IcmpHandle: THandle): Boolean; stdcall;

   TIcmpSendEcho = function(IcmpHandle:THandle;

   DestinationAddress: DWORD;

   RequestData: Pointer;

   RequestSize: Word;

   RequestOptions: PIPOptionInformation;

   ReplyBuffer: Pointer;

   ReplySize: DWord;

   Timeout: DWord

   ): DWord; stdcall;

   TMyPing = class(TForm)

   Panel1: TPanel;

   Label1: TLabel;

   PingEdit: TEdit;

   ExeBtn: TButton;

   Button2: TButton;

   Button3: TButton;

   StatusShow: TMemo;

   procedure Button3Click(Sender: TObject);

   procedure FormCreate(Sender: TObject);

   procedure ExeBtnClick(Sender: TObject);

   private

   { Private declarations }

   hICMP: THANDLE;

   IcmpCreateFile : TIcmpCreateFile;

   IcmpCloseHandle: TIcmpCloseHandle;

   IcmpSendEcho: TIcmpSendEcho;

   public

   { Public declarations }

  end;

  procedure TMyPing.FormCreate(Sender: TObject);

  var

   WSAData: TWSAData;

   hICMPdll: HMODULE;

  begin

   WSAStartup($101, WSAData);

   // Load the icmp.dll stuff

   hICMPdll := LoadLibrary('icmp.dll');

   @ICMPCreateFile := GetProcAddress(hICMPdll, 'IcmpCreateFile');

   @IcmpCloseHandle := GetProcAddress(hICMPdll, 'IcmpCloseHandle');

   @IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');

   hICMP := IcmpCreateFile;

   StatusShow.Text := '';

   StatusShow.Lines.Add('目的IP地址 字节数 返回时间(毫秒)');

  end;

  接下来,就要进行如下所示的Ping操作的实际编程过程了。

  procedure TMyPing.ExeBtnClick(Sender: TObject);

  var

   IPOpt:TIPOptionInformation;// IP Options for packet to send

   FIPAddress:DWORD;

   pReqData,pRevData:pChar;

   pIPE:pIcmpEchoReply;// ICMP Echo reply buffer

   FSize: DWORD;

   MyString:string;

   FTimeOut:DWORD;

   BufferSize:DWORD;

  begin

   if PingEdit.Text <> '' then

   begin

   FIPAddress := inet_addr(PChar(PingEdit.Text));

   FSize := 40;

   BufferSize := SizeOf(TICMPEchoReply) + FSize;

   GetMem(pRevData,FSize);

   GetMem(pIPE,BufferSize);

   FillChar(pIPE^, SizeOf(pIPE^), 0);

   pIPE^.Data := pRevData;

   MyString := 'Hello,World';

   pReqData := PChar(MyString);

   FillChar(IPOpt, Sizeof(IPOpt), 0);

   IPOpt.TTL := 64;

   FTimeOut := 4000;

   IcmpSendEcho(hICMP, FIPAddress, pReqData, Length(MyString), @IPOpt, pIPE, BufferSize, FTimeOut);

   if pReqData^ = pIPE^.Options.OptionsData^ then

   begin

   StatusShow.Lines.Add(PChar(PingEdit.Text) + ' ' +IntToStr(pIPE^.DataSize) + ' ' +IntToStr(pIPE^.RTT));

   end;

   FreeMem(pRevData);

   FreeMem(pIPE);

   end

  end;

  通过上面的编程,我们就实现了Ping功能的界面操作。实际上,ICMP协议的功能还有很多,都可以通过对Icmp.dll的函数调用来实现。



 
同意 kkyy ,还是用任意一个网络控件返回错误简单,也可以使用 sock 直接判断 ip 。
 
Ping是ICMP协议的一个功能,调用Icmp.dll可实现Ping功能。不用控件,给你一段代码。
放入四个普通控件:
PingEdit:TEdit
Label1:TLabel;
StatusShow:TMomo;
Exebtn:Tbutton;
自己改一下就行了。

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,winsock;//加入
type
PIPOptionInformation = ^TIPOptionInformation;
TIPOptionInformation=packed record
TTL: Byte;
TOS: Byte;
Flags: Byte;
OptionsSize: Byte;
OptionsData: PChar;
end;
PIcmpEchoReply = ^TIcmpEchoReply;
TIcmpEchoReply = packed record
Address: DWORD;
Status: DWORD;
RTT: DWORD;
DataSize:Word;
Reserved: Word;
Data: Pointer;
Options: TIPOptionInformation;
end;
TIcmpCreateFile = function: THandle; stdcall;
TIcmpCloseHandle = function(IcmpHandle: THandle): Boolean; stdcall;
TIcmpSendEcho =
function(IcmpHandle:THandle;
DestinationAddress:DWORD;
RequestData: Pointer;
RequestSize: Word;
RequestOptions: PIPOptionInformation;
ReplyBuffer: Pointer;
ReplySize: DWord;
Timeout: DWord ):DWord; stdcall;
TTMyPing = class(TForm)
PingEdit: TEdit;
Label1: TLabel;
StatusShow: TMemo;
exebtn: TButton;
procedure exebtnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
hICMP: THANDLE;
IcmpCreateFile : TIcmpCreateFile;
IcmpCloseHandle:TIcmpCloseHandle;
IcmpSendEcho: TIcmpSendEcho;
public
WSAData:TWSAData;
{ Public declarations }
end;
var
TMyPing: TTMyPing;
implementation
{$R *.DFM}
procedure TTMyPing.exebtnClick(Sender: TObject);
var
IPOpt:TIPOptionInformation;// IP
FIPAddress:DWORD;
pReqData,pRevData:pChar;
pIPE:pIcmpEchoReply; // ICMP
FSize: DWORD;
MyString:string;
FTimeOut:DWORD;
BufferSize:DWORD;
begin
if PingEdit.Text <> '' then
begin
FIPAddress:=inet_addr(pchar(Pingedit.text));
FSize := 40;
BufferSize := SizeOf(TICMPEchoReply) + FSize;
GetMem(pRevData,FSize);
GetMem(pIPE,BufferSize);
FillChar(pIPE^, SizeOf(pIPE^), 0);
pIPE^.Data := pRevData;
MyString := '----------------------';
pReqData := PChar(MyString);
FillChar(IPOpt, Sizeof(IPOpt), 0);
IPOpt.TTL := 64;
FTimeOut := 4000;
IcmpSendEcho(hICMP, FIPAddress, pReqData, Length(MyString),
@IPOpt, pIPE, BufferSize, FTimeOut);
try
try
if pReqData^ = pIPE^.Options.OptionsData^ then
begin
StatusShow.Lines.Add(PChar(PingEdit.Text) + '-----'
+IntToStr(pIPE^.DataSize) + '-----' +IntToStr(pIPE^.RTT));
end;
except
showmessage('没有找到Ip地址!');
end;
finally
FreeMem(pRevData);
FreeMem(pIPE);
end;
end
else
showmessage('请输入Ip地址');
end;

procedure TTMyPing.FormCreate(Sender: TObject);
var
hICMPdll: HMODULE;
begin
WSAStartup(2,WSAData);
hICMPdll := LoadLibrary('icmp.dll');// 装载icmp.dll
@ICMPCreateFile := GetProcAddress(hICMPdll, 'IcmpCreateFile');
@IcmpCloseHandle := GetProcAddress(hICMPdll, 'IcmpCloseHandle');
@IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');
hICMP :=IcmpCreateFile;
StatusShow.Text := '';
StatusShow.Lines.Add('目的IP地址-----字节数----返回时间(毫秒)');
end;

procedure TTMyPing.FormClose(Sender: TObject; var Action: TCloseAction);
begin
WSACleanup();//'wsock32.dll'中的一个
end;

end.
 
刚吃了团年饭,回来就看到各位帖子,先表示感谢,再研究.
 
小雨哥:如何用任意一个网络控件返回错误,请你举个例子,这100分你和各位热心朋友分了,我再把押箱底的45分送给你,咱们都过个畅快年!
另外,我机子delphi6,delphi7都有,且用ClientSocket已可获取网站服务器时间.
 
请自己看帖子。
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1605418
上面完成了判断是否已经连在 internet 的代码,直接用吧。
 
请看我的《网通器》软件,是最好的范例。http://www.to-happy.com
 
我有一个思路,那在edit设置初值.进行比较.结果自然分明!!
 
五种查询Internet连接状态[含IP]的方法


五种查询Internet连接状态[含IP]的方法
版本: 上传者: tomore 上传时间: 2002 三月 28 23:58 访问数: 2210 下载数: 0
开发语言:
大小: 4,011 字节
简介: 1.Powersock 控件法:
这种方法最简单,利用FastNet页的 Powersock控件的LocalIP属性即可判断:
if(Powersock1->LocalIP=="127.0.0.1"):在线
else:离线
特点:[1]判断连接状态,[2]获得本地IP。

2.使用URL.DLL的InetIsOffline(0) 函数:
Win2K:URL.DLL存放在SYSTEM32;
Win9x:URL.DLL存放在SYSTEM;
用GetSystemDirectory(...)得到系统目录。
InetIsOffline(0)返回值:
TRUE: 离线; FALSE:在线。
特点:判断连接状态。

3.WinSock编程法:见程序
特点:[1]判断连接状态;[2]获得本地IP和主机名。

4.WinInet.DLL的InternetGetConnectedState(&dwFlag,0)函数:
注意:为使用该函数,须在项目文件中加入:USELIB("WinInet.LIB")
特点:获得较详的连接描述!

5.RASAPI32.DLL的RasEnumConnections函数:
要使用该“枚举所有活动连接”函数,必须:
#include "ras.h"。

若连接数>0:本机当前已连入Internet;
否则: 本机当前未连入Internet;

源码如下,在[BCB5 + WIN2K + 拨号上网]下通过(N字头的为菜单项):

-------------Powersock控件法-----------------------------------------
void __fastcall TForm1::N11Click(TObject *Sender)
{
if(Powersock1->LocalIP=="127.0.0.1")
ShowMessage("未连接:"+Powersock1->LocalIP);
else ShowMessage("已连接:"+Powersock1->LocalIP);
}


-------------URL.DLL的InetIsOffline函数法----------------------------
HINSTANCE hDLL;
typedef bool __stdcall(*FUN)(int); 定义DLL函数指针FUN
FUN isOffLine;
void __fastcall TForm1::N21Click(TObject *Sender)
{
char Buffer[MAX_PATH];
GetSystemDirectory(Buffer,MAX_PATH);
hDLL=LoadLibrary((AnsiString(Buffer)+"/URL.DLL").c_str());
if(hDLL==NULL){ ShowMessage("Cannot load URL.DLL! Return... "); return; }
isOffLine=(FUN)GetProcAddress(hDLL,"InetIsOffline");
if(isOffLine==NULL){ ShowMessage("Cannot load InetIsOffline(int), Return..."); return; }
if(!isOffLine(0)) ShowMessage("已连接");
else ShowMessage("未连接");
FreeLibrary(hDLL);
}


------------WinSock法------------------------------------------------
void __fastcall TForm1::N31Click(TObject *Sender)
{
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested=MAKEWORD(1,1); Start up WinSock
WSAStartup(wVersionRequested,&wsaData);
-----------------------------------------
hostent *p; char *p2; char s[128];
gethostname(s,128); Get the computer name
p=gethostbyname(s);
p2=inet_ntoa(*((in_addr *)p->h_addr)); Get the IpAddress
-----------------------------------------
AnsiString LocationIP=p2;
if(LocationIP=="127.0.0.1")
ShowMessage("未连接:"+LocationIP);
else ShowMessage("已连接:"+LocationIP);
WSACleanup();
}


-----------WinInet.DLL的InternetGetConnectedState函数法----------------
void __fastcall TForm1::N41Click(TObject *Sender)
{
StaticText1->Caption=""; StaticText2->Caption=""; StaticText3->Caption="";
StaticText4->Caption=""; StaticText5->Caption=""; StaticText6->Caption="";
StaticText7->Caption="";
DWORD dwFlag;
InternetGetConnectedState(&dwFlag,0);
if(dwFlag & INTERNET_CONNECTION_MODEM) StaticText1->Caption="Yes"; MODEM连接
else StaticText1->Caption="No";
if(dwFlag & INTERNET_CONNECTION_LAN) StaticText2->Caption="Yes"; LAN连接
else StaticText2->Caption="No";
if(dwFlag & INTERNET_CONNECTION_PROXY) StaticText3->Caption="Yes"; 代理连接
else StaticText3->Caption="No";
---------检查是否连接-------------------------------------------
if(InternetGetConnectedState(NULL,0)) StaticText4->Caption="Yes"; 在线
else StaticText4->Caption="No";
if(dwFlag & INTERNET_CONNECTION_OFFLINE) StaticText5->Caption="Yes";//离线。注:不好用!
else StaticText5->Caption="No";
----------------------------------------------------------------
if(dwFlag & INTERNET_RAS_INSTALLED) StaticText6->Caption="Yes";
else StaticText6->Caption="No";
if(dwFlag & INTERNET_CONNECTION_CONFIGURED) StaticText7->Caption="Yes";
else StaticText7->Caption="No";
}
----------RASAPI32.DLL的RasEnumConnections函数法---------------------------
#include "ras.h"
void __fastcall TForm1::N51Click(TObject *Sender)
{
RASCONN RASconn[256]; 活动连接数组
DWORD BuffSize; 数组所占内存大小;
DWORD ConnNum; 活动连接数目
RASconn[0].dwSize=sizeof(RASCONN); 必须指定一个连接[数组元素]的内存大小;
BuffSize=sizeof(RASCONN)*256;
DWORD dw
 
顶部