delphi获取本机或者网关的互联网IP地址


本文整理自网络,侵删。

 有两种情况,一种是本机直接接入互联网,比如通过拨号或者ADSL;另一种是局域网通过一个网关或者路由接入互联网;尤其是第二种情况,如何在局域网内的一台机器上获得当前网关所取得的互联网IP地址呢?
--------------------------------------------------------------------------------
view plaincopy to clipboardprint?
unit Functions;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

function CheckInternetOnline: boolean; //检查互联网是否在线
function IsIP(S: string): boolean; //判断录入的字符串是否为IP
function GetLocalIP: string; //获取本机IP地址
function GetTheInternetLocalIP: string; //获取外网IP地址


implementation

uses
GetInternetLocalIP,WinInet,Winsock;

//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

function CheckInternetOnline: boolean; //检查互联网是否在线
var
ConnectState: DWORD;
StateSize: DWORD;
begin
ConnectState := 0;
StateSize := SizeOf(ConnectState);
Result := false;
//Use WinInet.pas;
if InternetQueryOption(nil, INTERNET_OPTION_CONNECTED_STATE, @ConnectState, StateSize) then
Result := (ConnectState and INTERNET_STATE_DISCONNECTED) <> 2;
if Result then
Result := InternetCheckConnection('http://www.microsoft.com/isapi/redir.dll?prd=ie&pver=6&ar=msnhome', 1, 0);
end;

//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
function IsIP(S: string): boolean; //判断录入的字符串是否为IP
var
S1, S2, S3, S4, sTemp: string;
K, I: integer;
begin
S1 := '';
S2 := '';
S3 := '';
S4 := '';
K := 0;
Result := false;
S := Trim(S);
if Length(S) < 7 then exit; //最小的IP长度也是7位0.0.0.0
for I := 0 to Length(S) do
if S[I] = '.' then
Inc(K);
if K <> 3 then
begin
Result := false;
exit;
end
else
begin
sTemp := S;
for I := Length(sTemp) downto 1 do
if sTemp[I] = '.' then
Delete(sTemp, I, 1);
for I := Length(sTemp) downto 1 do
if not (sTemp[I] in ['0'..'9']) then
begin
Result := false;
exit;
end;
K := Pos('.', S);
S1 := Copy(S, 1, K - 1);
Delete(S, 1, K);
K := Pos('.', S);
S2 := Copy(S, 1, K - 1);
Delete(S, 1, K);
K := Pos('.', S);
S3 := Copy(S, 1, K - 1);
Delete(S, 1, K);
S4 := S;
Result := (StrToInt(S1) >= 0) and (StrToInt(S1) <= 255) and
(StrToInt(S2) >= 0) and (StrToInt(S2) <= 255) and
(StrToInt(S3) >= 0) and (StrToInt(S3) <= 255) and
(StrToInt(S4) >= 0) and (StrToInt(S4) <= 255);
end;
end;
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

function GetLocalIP: string; //获取本机IP地址
type
TaPInAddr = array[0..255] of PInAddr; //Use Winsock.pas
PaPInAddr = ^TaPInAddr;
var
phe: PHostEnt;
pptr: PaPInAddr;
Buffer: array[0..63] of char;
i: integer;
GInitData: TWSADATA;
begin
wsastartup($101, GInitData);
result := '';
GetHostName(Buffer, SizeOf(Buffer));
phe := GetHostByName(buffer);
if not assigned(phe) then
exit;
pptr := PaPInAddr(Phe^.h_addr_list);
i := 0;
while pptr^[I] <> nil do
begin
result := Result + StrPas(inet_ntoa(pptr^[I]^)) + ',';
inc(i);
end;
Delete(Result, Length(Result), 1);
wsacleanup;
end;
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

function GetTheInternetLocalIP: string; //获取外网IP地址
begin
Result := '';
//Use GetInternetLocalIP.pas
if not Assigned(GetInternetLocalIPForm) then
if CheckInternetOnline then
try
Application.CreateForm(TGetInternetLocalIPForm, GetInternetLocalIPForm);
while GetInternetLocalIPForm.TheInternetLocalIP = 'E' do Application.ProcessMessages;
Result := GetInternetLocalIPForm.TheInternetLocalIP;
if not IsIP(Result) then
Result := '';
finally
FreeAndNil(GetInternetLocalIPForm);
end;
end;
--------------------------------------------------------------------------------
unit Functions;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

function CheckInternetOnline: boolean; //检查互联网是否在线
function IsIP(S: string): boolean; //判断录入的字符串是否为IP
function GetLocalIP: string; //获取本机IP地址
function GetTheInternetLocalIP: string; //获取外网IP地址


implementation

uses
GetInternetLocalIP,WinInet,Winsock;

//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

function CheckInternetOnline: boolean; //检查互联网是否在线
var
ConnectState: DWORD;
StateSize: DWORD;
begin
ConnectState := 0;
StateSize := SizeOf(ConnectState);
Result := false;
//Use WinInet.pas;
if InternetQueryOption(nil, INTERNET_OPTION_CONNECTED_STATE, @ConnectState, StateSize) then
Result := (ConnectState and INTERNET_STATE_DISCONNECTED) <> 2;
if Result then
Result := InternetCheckConnection('http://www.microsoft.com/isapi/redir.dll?prd=ie&pver=6&ar=msnhome', 1, 0);
end;

//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
function IsIP(S: string): boolean; //判断录入的字符串是否为IP
var
S1, S2, S3, S4, sTemp: string;
K, I: integer;
begin
S1 := '';
S2 := '';
S3 := '';
S4 := '';
K := 0;
Result := false;
S := Trim(S);
if Length(S) < 7 then exit; //最小的IP长度也是7位0.0.0.0
for I := 0 to Length(S) do
if S[I] = '.' then
Inc(K);
if K <> 3 then
begin
Result := false;
exit;
end
else
begin
sTemp := S;
for I := Length(sTemp) downto 1 do
if sTemp[I] = '.' then
Delete(sTemp, I, 1);
for I := Length(sTemp) downto 1 do
if not (sTemp[I] in ['0'..'9']) then
begin
Result := false;
exit;
end;
K := Pos('.', S);
S1 := Copy(S, 1, K - 1);
Delete(S, 1, K);
K := Pos('.', S);
S2 := Copy(S, 1, K - 1);
Delete(S, 1, K);
K := Pos('.', S);
S3 := Copy(S, 1, K - 1);
Delete(S, 1, K);
S4 := S;
Result := (StrToInt(S1) >= 0) and (StrToInt(S1) <= 255) and
(StrToInt(S2) >= 0) and (StrToInt(S2) <= 255) and
(StrToInt(S3) >= 0) and (StrToInt(S3) <= 255) and
(StrToInt(S4) >= 0) and (StrToInt(S4) <= 255);
end;
end;
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

function GetLocalIP: string; //获取本机IP地址
type
TaPInAddr = array[0..255] of PInAddr; //Use Winsock.pas
PaPInAddr = ^TaPInAddr;
var
phe: PHostEnt;
pptr: PaPInAddr;
Buffer: array[0..63] of char;
i: integer;
GInitData: TWSADATA;
begin
wsastartup($101, GInitData);
result := '';
GetHostName(Buffer, SizeOf(Buffer));
phe := GetHostByName(buffer);
if not assigned(phe) then
exit;
pptr := PaPInAddr(Phe^.h_addr_list);
i := 0;
while pptr^[I] <> nil do
begin
result := Result + StrPas(inet_ntoa(pptr^[I]^)) + ',';
inc(i);
end;
Delete(Result, Length(Result), 1);
wsacleanup;
end;
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

function GetTheInternetLocalIP: string; //获取外网IP地址
begin
Result := '';
//Use GetInternetLocalIP.pas
if not Assigned(GetInternetLocalIPForm) then
if CheckInternetOnline then
try
Application.CreateForm(TGetInternetLocalIPForm, GetInternetLocalIPForm);
while GetInternetLocalIPForm.TheInternetLocalIP = 'E' do Application.ProcessMessages;
Result := GetInternetLocalIPForm.TheInternetLocalIP;
if not IsIP(Result) then
Result := '';
finally
FreeAndNil(GetInternetLocalIPForm);
end;
end;
--------------------------------------------------------------------------------

1)简单。
2)论坛当中很多了。访问一个外部网址,得到互联网IP地址呢

相关阅读 >>

Delphi 取得txt文件编码

Delphi 显示程序占用内存多少

Delphi 输入年月判断天数

Delphi trayicon1 托盘气泡提示

Delphi10.3.1安卓照相

Delphi 采集功能代码getstr

Delphi 根据文件扩展名判断文件类型函数写法

Delphi 截取字符串的用法

Delphi 监视剪贴板内容

Delphi获取flash文件的影片时长,原始尺寸,帧数等信息

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...