如何用软件的方法知道本机已连上internet网?(50分)

  • 主题发起人 主题发起人 sunstone
  • 开始时间 开始时间
用软件的话,直接用ping你的isp就可以了
用程序的话,用ras api就可以实现
 
补充说一点:
用编程序的方法实现.

dnchen:
您说的ras api是什麽?能否给我email一份?或下载地址?
sunstone@263.net

 
用个ICMP控件直接在程序里ping吧
不用控件也简单. 用api一样
uses winsock;

p_ip_options=^t_ip_options;
t_ip_options=packed record
Ttl: byte;
Tos: byte;
flags: byte;
optionssize: byte;
optionsdata: pointer;
end;

p_icmp_echo_reply=^t_icmp_echo_reply;
t_icmp_echo_reply=packed record
address: u_long;
status: u_long;
rttime: u_long;
datasize: word;
reserved: word;
data: pointer;
ip_options:t_ip_options;
end;

function icmpcreatefile: THandle; stdcall; external 'icmp.dll' name 'ICMPCreateFile';

function ICMPSendEcho:function(ICMPHandle: THandle;
DestAddress:longint;
Requestdata:pointer;
requestsize:word;
RequestOptns: p_ip_options;
ReplyBuffer:pointer;
Replysize:dword;
Timeout: DWord
):dword; stdcall; external 'icmp.dll' name 'ICMPSendEcho';

function ICMPCloseHandle:function(ICMPHandle:THandle):Boolean; stdcall; external 'icmp.dll' name 'ICMPCloseHandle';

function Ping(Ip: string): boolean;
var
f_ICMP_Handle: THandle;
f_address: LongInt;
f_timeout: Integer;
f_blocksize: Integer;
f_replysize: Integer;
requestoptions: T_ip_options;
reqyestdatam replybuffer: Pointer;
begin
f_ICMP_Handle:=icmpcreatefile;
f_timeout:=1000;
f_blocksize:=64;
f_replysize:=16+sizeof(t_icmp_echo_reply)+f_blocksize;
GetMem(requestdata,f_blocksize);
fillchar(requestdata^,64,#$32);
GetMem(replybuffer,f_replysize);
requestoptions.ttl:=255;
requestoptions.tos:=0;
requestoptions.flags:=0;
requestoptions.optionssize:=0;
requestoptions.optionsdata:=NIL;


f_address := Inet_Addr(PChar(Ip));

if f_address = SOCKET_ERROR then
begin
result := false;
ICMPCloseHandle(f_icmp_handle);
exit;
end;

if ICMPSendEcho(f_icmp_handle,f_address,
requestdata,f_blocksize,
@requestoptions,
replybuffer,f_replysize,
f_timeout)<>1 then
result := false
else
result := true;
icmpclosehandle(f_icmp_handle);
end;
 
1. rasapi控件我已找到.但不会用?有中文说明吗?
2. windows登陆internet后,使用什麽消息变量传递的.我可否使用
响应windows消息的方法,来知道windows已登陆到internent?
不要使用timer元件来监测,它太占用资源了.要详细的delphi程序
代码.

那位大虾知道,请告诉我,可以再加50分!!!
 
这个问题不是刚讨论过吗?
pega说,
监视本机IP的变化,如果挂上了internet,就会多出一个新的IP来.
(是局域网就歇菜了.)


还有老外由这个思路写了控件了.不长,我就直接贴出来了.

{ ****************************************************************** }
{ }
{ VCL component TKZInternetDetect }
{ }
{ Internet detector component }
{ }
{ Code generated by Component Create for Delphi }
{ }
{ Generated from source file d:/compon~1/intern~1/d3/kzintdet.cd }
{ on 7 July 1999 at 21:52 }
{ }
{ Copyright ?1999 by Kire Zvezdakoski }
{ }
{ ****************************************************************** }

unit KZIntDet;

interface

uses Messages, SysUtils, Classes, Controls, Forms, Graphics,
Windows, Extctrls, RAS;

type
TKZInternetDetect = class(TComponent)
private
{ Private fields of TKZInternetDetect }
FCheckInterval : Integer;
FConnected : Boolean;
FIP : String;
{ Pointer to application's OnConnect handler, if any }
FOnConnect : TNotifyEvent;
{ Pointer to application's OnDisconnect handler, if any }
FOnDisconnect : TNotifyEvent;
Timer : TTimer;

{ Private methods of TKZInternetDetect }
{ Method to set variable and property values and create objects }
procedure AutoInitialize;
{ Method to free any objects created by AutoInitialize }
procedure AutoDestroy;
procedure SetConnected(Value : Boolean);
procedure SetIP(Value : String);
procedure Check(Sender:TObject);

protected
{ Protected fields of TKZInternetDetect }

{ Protected methods of TKZInternetDetect }
{ Method to generate OnConnect event }
procedure Connect(Sender : TObject); virtual;
{ Method to generate OnDisconnect event }
procedure Disconnect(Sender : TObject); virtual;
procedure Loaded; override;

public
{ Public fields and properties of TKZInternetDetect }

{ Public methods of TKZInternetDetect }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function Execute : Boolean;

published
{ Published properties of TKZInternetDetect }
property OnConnect : TNotifyEvent read FOnConnect write FOnConnect;
property OnDisconnect : TNotifyEvent read FOnDisconnect write FOnDisconnect;
property CheckInterval : Integer
read FCheckInterval write FCheckInterval
default 1000;
property Connected : Boolean
read FConnected write SetConnected
default False;
property IP : String read FIP write SetIP;

end;

procedure Register;

implementation

procedure Register;
begin
{ Register TKZInternetDetect with KireZ as its
default page on the Delphi component palette }
RegisterComponents('KireZ', [TKZInternetDetect]);
end;

{ Method to set variable and property values and create objects }
procedure TKZInternetDetect.AutoInitialize;
begin
Timer := TTimer.Create(Self);
FCheckInterval := 1000;
FConnected := False;
FIP := '127.0.0.1';
end; { of AutoInitialize }

{ Method to free any objects created by AutoInitialize }
procedure TKZInternetDetect.AutoDestroy;
begin
Timer.Free;
end; { of AutoDestroy }

procedure TKZInternetDetect.SetConnected(Value : Boolean);
begin
FConnected := Value;
end;

procedure TKZInternetDetect.SetIP(Value : String);
begin
FIP := Value;
end;

{ Method to generate OnConnect event }
procedure TKZInternetDetect.Connect(Sender : TObject);
begin
{ Has the application assigned a method to the event, whether
via the Object Inspector or a run-time assignment? If so,
execute that method }
if Assigned(FOnConnect) then
FOnConnect(Sender);
end;

{ Method to generate OnDisconnect event }
procedure TKZInternetDetect.Disconnect(Sender : TObject);
begin
{ Has the application assigned a method to the event, whether
via the Object Inspector or a run-time assignment? If so,
execute that method }
if Assigned(FOnDisconnect) then
FOnDisconnect(Sender);
end;

procedure TKZInternetDetect.Check(Sender:TObject);
var
RASConns:TRasConn;
dwSize:dword;
dwCount:dword;
RASpppIP:TRASpppIP;
T:Boolean;
r:string;
begin
r:='';
RASConns.dwSize:=SizeOf(TRASConn);
RASpppIP.dwSize:=SizeOf(RASpppIP);
dwSize:=SizeOf(RASConns);
If RASEnumConnections(@RASConns, dwSize, dwCount)=0 Then
If dwCount > 0 Then begin
dwSize := SizeOf(RASpppIP);
If RASGetProjectionInfo(RASConns.hRasConn, RASP_PPPIP, @RasPPPIP, dwSize)=0 Then
r:=strPas(RASpppIP.szIPAddress);
End;
t:=Connected;
Connected:=(r<>'127.0.0.1') and (r<>'') and (r<>' ');
FIP:=r;
if ((not t) and Connected) then Connect(Sender);
if (t and (not Connected)) then Disconnect(Sender);
end;

constructor TKZInternetDetect.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
AutoInitialize;

{ Code to perform other tasks when the component is created }
Timer.OnTimer:=Check;
CheckInterval:=1000;
end;

destructor TKZInternetDetect.Destroy;
begin
AutoDestroy;
inherited Destroy;
end;

function TKZInternetDetect.Execute : Boolean;
begin
{ Perform the component operation }

{ Return True if the operation was successful, False otherwise }
Result := True
end;

procedure TKZInternetDetect.Loaded;
begin
inherited Loaded;

{ Perform any component setup that depends on the property
values having been set }

end;


end.
 
>1. rasapi控件我已找到.但不会用?有中文说明吗?
请看msdn里的资料
>2. windows登陆internet后,使用什麽消息变量传递的.我可否使用
没有消息可以响应,只有你自己的程序拨号时,才可以提供一个回调函数给ras api,
这个时候才可以获得拨号成功/失败信息。
 
可以通过ping www.netscape.com是否通来判断, 不过, 如果你的局域网
有一个机器或服务器是WWW.NETSCAPE.COM, 那么......
话说回来了, 谁会设这么一台机器呢?

可以用ics构件里的tping构件.
 
使用 idle 是否会比用 timer 节约资源呢?
不过我以前就是用 timer 来实现的一个记费器,并没感觉事件响应有迟缓。
其实 timer 也不是太占资源啦!
 
多人接受答案了。
 
后退
顶部