检测网络连接的类。 检测 www.google.cn 连接是外网,不能连接是内网 方法 Execute: 调用我就好了,其它的交给我去办 属性 Busy: 我正忙着呢,还没检测完,一会儿再来 AccessType: 你希望我怎么连接网络,直接连还是用代理? AsynRequest: 同步检测的话我们要有一会儿看不到对方了,呜呜 UserAgent: 想告诉Web服务器是阿猫还是阿狗在连接?在这里设置就好了 Proxy: 让我用代理连网络,用哪个代理啊? Url: 原来是要检测这个地址能不能访问,保证办到 事件 OnStart: 开始检测之前我会在这里通知你一声 OnStatusChange:检测过程中发生什么事我会在这里及时通知你的 OnComplete: 检测完成了,成功不成功我都会告诉你 {**********************************************************} { } { TZoCInetChecker Component Version 1.00 } { } { Function: Asynchronously Check if a given web page } { can be successfully retreived and feed back } { the user with callback status infomation } { } { } { This is a freeware. If you made cool changes on it, } { please send them to me. } { } { Email: [email protected] } { URL: http://www.ZoCsoft.com } { } { History: + New Feature, - Removed, * Modified } { } { version 1.00 2005-06-03 } { The first version } { } {**********************************************************} unit ZoCInetChecker; interface uses Windows, Messages, SysUtils, Classes, WinInet; const INTERNET_STATUS_DETECTING_PROXY = 80; {$EXTERNALSYM INTERNET_STATUS_DETECTING_PROXY} WM_STATUSCHANGE = WM_USER + 200; WM_CHECKCOMPLETE = WM_USER + 201; type LPINTERNET_ASYNC_RESULT = ^INTERNET_ASYNC_RESULT; INTERNET_ASYNC_RESULT = record dwResult: DWORD; dwError: DWORD; end; pREQUEST_CONTEXT = ^REQUEST_CONTEXT; REQUEST_CONTEXT = record hWindow: HWND; hOpen: HINTERNET; //HINTERNET handle created by InternetOpen end; TOnStatusChangeEvent = procedure(Sender: TObject; StatusCode: Cardinal) of object; TOnCompleteEvent = procedure(Sender: TObject; Connected: Boolean) of object; TAccessType = (atDirectConnect, atPreConfig, atPreConfigWithNoProxy, atProxy); TZoCInetChecker = class(TComponent) private { Private declarations } FUrl: string; FAccessType: TAccessType; FProxy: string; FOnStart: TNotifyEvent; FOnStatusChange: TOnStatusChangeEvent; FOnComplete: TOnCompleteEvent; FBusy: Boolean; hOpenUrl, hOpen: HINTERNET; FUserAgent: string; FWindowHandle: HWnd; iscCallback: INTERNET_STATUS_CALLBACK; RC: REQUEST_CONTEXT; FAsynRequest: Boolean; procedure WndProc(var Msg: TMessage); protected { Protected declarations } procedure DoOnStatusChange(StatusCode: Cardinal); dynamic; public { Public declarations } property Busy: Boolean read FBusy write FBusy; function Execute: Boolean; constructor Create(AOwner: TComponent); override; destructor Destroy; override; published { Published declarations } property AccessType: TAccessType read FAccessType write FAccessType default atDirectConnect; property AsynRequest: Boolean read FAsynRequest write FAsynRequest default True; property UserAgent: string read FUserAgent write FUserAgent; property Proxy: string read FProxy write FProxy; property Url: string read FUrl write FUrl; property OnStart: TNotifyEvent read FOnStart write FOnStart; property OnStatusChange: TOnStatusChangeEvent read FOnStatusChange write FOnStatusChange; property OnComplete: TOnCompleteEvent read FOnComplete write FOnComplete; end; procedure Register; function StatusCode2StatusText(StatusCode: Cardinal): string; implementation procedure Register; begin RegisterComponents('ZoC', [TZoCInetChecker]); end;
{ TZoCInetChecker } procedure InternetCallback(hInternet: HINTERNET; dwContext: DWORD; dwInternetStatus: DWORD; lpvStatusInformation: hwnd; dwStatusInformationLength: DWORD); stdcall; var cpContext : pREQUEST_CONTEXT; FConnected : Boolean; dwindex, dwcodelen : dword; dwcode : array[1..20] of char; res : pchar; begin cpContext := pREQUEST_CONTEXT(dwContext); PostMessage(cpContext^.hWindow, WM_STATUSCHANGE, dwInternetStatus, 0); if dwInternetStatus = INTERNET_STATUS_REQUEST_COMPLETE then begin dwIndex := 0; dwCodeLen := 10; HttpQueryInfo(Pointer(LPINTERNET_ASYNC_RESULT(lpvStatusInformation).dwResult), HTTP_QUERY_STATUS_CODE, @dwcode, dwcodeLen, dwIndex); res := pchar(@dwcode); //HTTP_STATUS_OK 200 The request completed successfully //HTTP_STATUS_REDIRECT 302 The requested resource resides temporarily under a different URI FConnected := (res = '200') or (res = '302'); InternetCloseHandle(Pointer(LPINTERNET_ASYNC_RESULT(lpvStatusInformation).dwResult)); InternetCloseHandle(cpContext^.hOpen); PostMessage(cpContext^.hWindow, WM_CHECKCOMPLETE, Integer(FConnected), 0); end; end; constructor TZoCInetChecker.Create(AOwner: TComponent); begin inherited Create(AOwner); FWindowHandle := AllocateHWnd(WndProc); FAccessType := atDirectConnect; FAsynRequest := True; FUserAgent := 'ZoCSoft Internet Checker 1.0'; end; destructor TZoCInetChecker.Destroy; begin InternetCloseHandle(hOpenUrl); if FWindowHandle <> 0 then DeallocateHWnd(FWindowHandle); inherited Destroy; end; procedure TZoCInetChecker.DoOnStatusChange(StatusCode: Cardinal); begin if Assigned(FOnStatusChange) then FOnStatusChange(Self, StatusCode); end; function TZoCInetChecker.Execute: Boolean; var Flags : Cardinal; begin if FUrl = '' then Exit; FBusy := True; if Assigned(FOnStart) then FOnStart(Self); if FAsynRequest then Flags := INTERNET_FLAG_ASYNC else Flags := 0; case FAccessType of atDirectConnect: hOpen := InternetOpen(PChar(FUserAgent), INTERNET_OPEN_TYPE_DIRECT, nil, nil, Flags); atPreConfig: hOpen := InternetOpen(PChar(FUserAgent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, Flags); atPreConfigWithNoProxy: hOpen := InternetOpen(PChar(FUserAgent), INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY, nil, nil, Flags); atProxy: hOpen := InternetOpen(PChar(FUserAgent), INTERNET_OPEN_TYPE_PROXY, PChar(FProxy), nil, Flags); end; RC.hWindow := FWindowHandle; RC.hOpen := hOpen; if FAsynRequest then iscCallback := InternetSetStatusCallback(hOpen, INTERNET_STATUS_CALLBACK(@InternetCallback)); hOpenUrl := InternetOpenUrl(hOpen, PChar(FUrl), nil, 0, INTERNET_FLAG_RELOAD, Cardinal(@RC)); Result := hOpenUrl <> nil; end; procedure TZoCInetChecker.WndProc(var Msg: TMessage); begin case Msg.Msg of WM_STATUSCHANGE: DoOnStatusChange(Msg.WParam); WM_CHECKCOMPLETE: begin FBusy := True; if Assigned(FOnComplete) then FOnComplete(Self, Boolean(Msg.WParam)); end; else Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam); end; end; function StatusCode2StatusText(StatusCode: Cardinal): string; begin case StatusCode of INTERNET_STATUS_CLOSING_CONNECTION: Result := 'Closing connection to the server.'; INTERNET_STATUS_CONNECTED_TO_SERVER: Result := 'Successfully connected to the socket address.'; INTERNET_STATUS_CONNECTING_TO_SERVER: Result := 'Connecting to the socket address.'; INTERNET_STATUS_CONNECTION_CLOSED: Result := 'Closed the connection to the server.'; INTERNET_STATUS_DETECTING_PROXY: Result := 'Proxy has been detected.'; INTERNET_STATUS_HANDLE_CLOSING: Result := 'Handle value has been terminated.'; INTERNET_STATUS_HANDLE_CREATED: Result := 'InternetConnect has created a new handle.'; INTERNET_STATUS_INTERMEDIATE_RESPONSE: Result := 'Received status code message from the server.'; INTERNET_STATUS_NAME_RESOLVED: Result := 'Successfully found the IP address.'; INTERNET_STATUS_RECEIVING_RESPONSE: Result := 'Waiting for the server to respond.'; INTERNET_STATUS_REDIRECT: Result := 'Request is about to be redirected.'; INTERNET_STATUS_REQUEST_COMPLETE: Result := 'Completed.'; INTERNET_STATUS_REQUEST_SENT: Result := 'Successfully sent the information request to the server.'; INTERNET_STATUS_RESOLVING_NAME: Result := 'Looking up the IP address of the name.'; INTERNET_STATUS_RESPONSE_RECEIVED: Result := 'Successfully received a response from the server.'; INTERNET_STATUS_SENDING_REQUEST: Result := 'Sending the information request to the server.'; INTERNET_STATUS_STATE_CHANGE: Result := 'Security State Change.'; else Result := 'Unknown Status.'; end; end; end.
1)你处在要判断的局网内;(这种可能应该不大,真是这样你没必要作这种判断)
2)你处在INTERNET上,你可以通过某种方式知道别人服务器的IP
那么咱们说下IP分类
IP分为A,B,C类地址
比如10.*.*.*就是A类
比如10.1.*.*就是B类
比如192.168.1.*就是C类
单纯依靠是哪类地址来判断是否服务器在LAN或者WAN是无法做到的.
那么很简单通过WAN你是可以PING的通,而LAN的异网IP你是PING不通的,
其实你如果真能取得异网内某IP的地址,应该完全可以取得它的DNS,网关的IP,这样
就取得了它在当前局网的外部IP了.
else ShowMessage('不能连接Internet');
{实现上翻、下翻、到某页、首页、末页}
{还是那句话,有点小问题,是细节问题,自己改}unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
Button2: TButton;
Button3: TButton;
Edit1: TEdit;
Button4: TButton;
Button5: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function GridPageScroll: boolean;
end;var
Form1: TForm1;implementation{$R *.dfm}var
SS: TStringList;
Page: integer = 1;//当前页码
PerPage: integer = 4;//每页行数function TForm1.GridPageScroll: boolean;
var
SS2: TStringList;
I, F, T, L : integer;
begin
Result := True;
if (Page<1) or (Page>(SS.Count div PerPage)+1) then begin
Result := False;
Exit;
end;
SS2 := TStringList.Create;
F := Page * PerPage - PerPage;
T := Page * PerPage;
if Page * PerPage > SS.Count-1 then T := SS.Count-1;
L := 1;
for I := F to T do begin
SS2.Text := StringReplace(SS[I], ',', #10, [rfReplaceAll]);
StringGrid1.Cells[1,L] := SS2[0];
StringGrid1.Cells[2,L] := SS2[1];
Inc(L);
end;
SS2.Free;
Edit1.Text := InttoStr(Page);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if not GridPageScroll then begin
Showmessage('已到尾');
Dec(Page);
end else Inc(Page);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
if not GridPageScroll then begin
Showmessage('已到头');
Inc(Page);
end else Dec(Page);
end;procedure TForm1.Button3Click(Sender: TObject);
begin
Page := StrtoInt(Edit1.Text);
GridPageScroll;
end;procedure TForm1.Button4Click(Sender: TObject);
begin
Page := 1;
GridPageScroll;
end;procedure TForm1.Button5Click(Sender: TObject);
begin
Page := SS.Count div PerPage + 1;//这句有问题,自己改
GridPageScroll;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption := '向下翻';
Button2.Caption := '向上翻';
Button3.Caption := '跳到某页';
Button4.Caption := '首页';
Button5.Caption := '末页';
Edit1.Text := '1';
StringGrid1.RowCount := PerPage+1;
SS := TStringList.Create;
SS.LoadFromFile('c:\test.txt');
GridPageScroll;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
if SS<>nil then FreeAndNil(SS);
end;end.
你可以考虑加入一个通讯层专门负责数据传输,而不要依赖数据库客户端和服务器的自建通讯。
检测 www.google.cn 连接是外网,不能连接是内网
方法
Execute: 调用我就好了,其它的交给我去办
属性
Busy: 我正忙着呢,还没检测完,一会儿再来
AccessType: 你希望我怎么连接网络,直接连还是用代理?
AsynRequest: 同步检测的话我们要有一会儿看不到对方了,呜呜
UserAgent: 想告诉Web服务器是阿猫还是阿狗在连接?在这里设置就好了
Proxy: 让我用代理连网络,用哪个代理啊?
Url: 原来是要检测这个地址能不能访问,保证办到
事件
OnStart: 开始检测之前我会在这里通知你一声
OnStatusChange:检测过程中发生什么事我会在这里及时通知你的
OnComplete: 检测完成了,成功不成功我都会告诉你
{**********************************************************}
{ }
{ TZoCInetChecker Component Version 1.00 }
{ }
{ Function: Asynchronously Check if a given web page }
{ can be successfully retreived and feed back }
{ the user with callback status infomation }
{ }
{ }
{ This is a freeware. If you made cool changes on it, }
{ please send them to me. }
{ }
{ Email: [email protected] }
{ URL: http://www.ZoCsoft.com }
{ }
{ History: + New Feature, - Removed, * Modified }
{ }
{ version 1.00 2005-06-03 }
{ The first version }
{ }
{**********************************************************}
unit ZoCInetChecker;
interface
uses
Windows, Messages, SysUtils, Classes, WinInet;
const
INTERNET_STATUS_DETECTING_PROXY = 80;
{$EXTERNALSYM INTERNET_STATUS_DETECTING_PROXY}
WM_STATUSCHANGE = WM_USER + 200;
WM_CHECKCOMPLETE = WM_USER + 201;
type
LPINTERNET_ASYNC_RESULT = ^INTERNET_ASYNC_RESULT;
INTERNET_ASYNC_RESULT = record
dwResult: DWORD;
dwError: DWORD;
end;
pREQUEST_CONTEXT = ^REQUEST_CONTEXT;
REQUEST_CONTEXT = record
hWindow: HWND;
hOpen: HINTERNET; //HINTERNET handle created by InternetOpen
end;
TOnStatusChangeEvent = procedure(Sender: TObject; StatusCode: Cardinal) of object;
TOnCompleteEvent = procedure(Sender: TObject; Connected: Boolean) of object;
TAccessType = (atDirectConnect, atPreConfig, atPreConfigWithNoProxy, atProxy);
TZoCInetChecker = class(TComponent)
private
{ Private declarations }
FUrl: string;
FAccessType: TAccessType;
FProxy: string;
FOnStart: TNotifyEvent;
FOnStatusChange: TOnStatusChangeEvent;
FOnComplete: TOnCompleteEvent;
FBusy: Boolean;
hOpenUrl, hOpen: HINTERNET;
FUserAgent: string;
FWindowHandle: HWnd;
iscCallback: INTERNET_STATUS_CALLBACK;
RC: REQUEST_CONTEXT;
FAsynRequest: Boolean;
procedure WndProc(var Msg: TMessage);
protected
{ Protected declarations }
procedure DoOnStatusChange(StatusCode: Cardinal); dynamic;
public
{ Public declarations }
property Busy: Boolean read FBusy write FBusy;
function Execute: Boolean;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property AccessType: TAccessType read FAccessType write FAccessType
default atDirectConnect;
property AsynRequest: Boolean read FAsynRequest write FAsynRequest
default True;
property UserAgent: string read FUserAgent write FUserAgent;
property Proxy: string read FProxy write FProxy;
property Url: string read FUrl write FUrl;
property OnStart: TNotifyEvent read FOnStart write FOnStart;
property OnStatusChange: TOnStatusChangeEvent read FOnStatusChange write
FOnStatusChange;
property OnComplete: TOnCompleteEvent read FOnComplete write FOnComplete;
end;
procedure Register;
function StatusCode2StatusText(StatusCode: Cardinal): string;
implementation
procedure Register;
begin
RegisterComponents('ZoC', [TZoCInetChecker]);
end;
{ TZoCInetChecker }
procedure InternetCallback(hInternet: HINTERNET; dwContext: DWORD;
dwInternetStatus: DWORD; lpvStatusInformation: hwnd;
dwStatusInformationLength: DWORD); stdcall;
var
cpContext : pREQUEST_CONTEXT;
FConnected : Boolean;
dwindex, dwcodelen : dword;
dwcode : array[1..20] of char;
res : pchar;
begin
cpContext := pREQUEST_CONTEXT(dwContext);
PostMessage(cpContext^.hWindow, WM_STATUSCHANGE, dwInternetStatus, 0);
if dwInternetStatus = INTERNET_STATUS_REQUEST_COMPLETE then
begin
dwIndex := 0;
dwCodeLen := 10;
HttpQueryInfo(Pointer(LPINTERNET_ASYNC_RESULT(lpvStatusInformation).dwResult),
HTTP_QUERY_STATUS_CODE, @dwcode, dwcodeLen, dwIndex);
res := pchar(@dwcode);
//HTTP_STATUS_OK 200 The request completed successfully
//HTTP_STATUS_REDIRECT 302 The requested resource resides temporarily under a different URI
FConnected := (res = '200') or (res = '302');
InternetCloseHandle(Pointer(LPINTERNET_ASYNC_RESULT(lpvStatusInformation).dwResult));
InternetCloseHandle(cpContext^.hOpen);
PostMessage(cpContext^.hWindow, WM_CHECKCOMPLETE, Integer(FConnected), 0);
end;
end;
constructor TZoCInetChecker.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FWindowHandle := AllocateHWnd(WndProc);
FAccessType := atDirectConnect;
FAsynRequest := True;
FUserAgent := 'ZoCSoft Internet Checker 1.0';
end;
destructor TZoCInetChecker.Destroy;
begin
InternetCloseHandle(hOpenUrl);
if FWindowHandle <> 0 then
DeallocateHWnd(FWindowHandle);
inherited Destroy;
end;
procedure TZoCInetChecker.DoOnStatusChange(StatusCode: Cardinal);
begin
if Assigned(FOnStatusChange) then
FOnStatusChange(Self, StatusCode);
end;
function TZoCInetChecker.Execute: Boolean;
var
Flags : Cardinal;
begin
if FUrl = '' then
Exit;
FBusy := True;
if Assigned(FOnStart) then
FOnStart(Self);
if FAsynRequest then
Flags := INTERNET_FLAG_ASYNC
else
Flags := 0;
case FAccessType of
atDirectConnect:
hOpen := InternetOpen(PChar(FUserAgent), INTERNET_OPEN_TYPE_DIRECT,
nil, nil, Flags);
atPreConfig:
hOpen := InternetOpen(PChar(FUserAgent), INTERNET_OPEN_TYPE_PRECONFIG,
nil, nil, Flags);
atPreConfigWithNoProxy:
hOpen := InternetOpen(PChar(FUserAgent),
INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY,
nil, nil, Flags);
atProxy:
hOpen := InternetOpen(PChar(FUserAgent), INTERNET_OPEN_TYPE_PROXY,
PChar(FProxy), nil, Flags);
end;
RC.hWindow := FWindowHandle;
RC.hOpen := hOpen;
if FAsynRequest then
iscCallback := InternetSetStatusCallback(hOpen,
INTERNET_STATUS_CALLBACK(@InternetCallback));
hOpenUrl := InternetOpenUrl(hOpen, PChar(FUrl), nil, 0,
INTERNET_FLAG_RELOAD, Cardinal(@RC));
Result := hOpenUrl <> nil;
end;
procedure TZoCInetChecker.WndProc(var Msg: TMessage);
begin
case Msg.Msg of
WM_STATUSCHANGE:
DoOnStatusChange(Msg.WParam);
WM_CHECKCOMPLETE:
begin
FBusy := True;
if Assigned(FOnComplete) then
FOnComplete(Self, Boolean(Msg.WParam));
end;
else
Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
end;
end;
function StatusCode2StatusText(StatusCode: Cardinal): string;
begin
case StatusCode of
INTERNET_STATUS_CLOSING_CONNECTION:
Result := 'Closing connection to the server.';
INTERNET_STATUS_CONNECTED_TO_SERVER:
Result := 'Successfully connected to the socket address.';
INTERNET_STATUS_CONNECTING_TO_SERVER:
Result := 'Connecting to the socket address.';
INTERNET_STATUS_CONNECTION_CLOSED:
Result := 'Closed the connection to the server.';
INTERNET_STATUS_DETECTING_PROXY:
Result := 'Proxy has been detected.';
INTERNET_STATUS_HANDLE_CLOSING:
Result := 'Handle value has been terminated.';
INTERNET_STATUS_HANDLE_CREATED:
Result := 'InternetConnect has created a new handle.';
INTERNET_STATUS_INTERMEDIATE_RESPONSE:
Result := 'Received status code message from the server.';
INTERNET_STATUS_NAME_RESOLVED:
Result := 'Successfully found the IP address.';
INTERNET_STATUS_RECEIVING_RESPONSE:
Result := 'Waiting for the server to respond.';
INTERNET_STATUS_REDIRECT:
Result := 'Request is about to be redirected.';
INTERNET_STATUS_REQUEST_COMPLETE:
Result := 'Completed.';
INTERNET_STATUS_REQUEST_SENT:
Result := 'Successfully sent the information request to the server.';
INTERNET_STATUS_RESOLVING_NAME:
Result := 'Looking up the IP address of the name.';
INTERNET_STATUS_RESPONSE_RECEIVED:
Result := 'Successfully received a response from the server.';
INTERNET_STATUS_SENDING_REQUEST:
Result := 'Sending the information request to the server.';
INTERNET_STATUS_STATE_CHANGE:
Result := 'Security State Change.';
else
Result := 'Unknown Status.';
end;
end;
end.