如果你是直接的ADSL上网,可以通过这个//取IP地址 function Tfrm_server.GetIP : string; type TaPInAddr = array [0..10] of PInAddr; 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 phe = nil then Exit; pptr := PaPInAddr(Phe^.h_addr_list); I := 0; while pptr^[I] <> nil do begin result:=StrPas(inet_ntoa(pptr^[I]^)); Inc(I); end; WSACleanup; end; 或者使用NMDayTime1组件也可以的.我就是这样使用的了
做个asp的网页放到外部网上,再用你的程序访问该网页就可以得到你的外网ip了
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,winsock;type TaPInAddr = array[0..10] of PInAddr; PaPInAddr = ^TaPInAddr; TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; Label1: TLabel; Label2: TLabel; procedure Button1Click(Sender: TObject); private function getIPs: tStrings; { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}function TForm1.getIPs: tStrings; var phe: PHostEnt; pptr: PaPInAddr; Buffer: array[0..63] of Char; I: Integer; GInitData: TWSAData; begin WSAStartup($101,GInitData); Result := TStringList.Create; Result.Clear; GetHostName(Buffer,SizeOf(Buffer)); phe := GetHostByName(buffer); if phe = nil then begin Exit; end; pPtr := PaPInAddr(phe^.h_addr_list); I := 0; while pPtr^[I] <> nil do begin Result.Add(inet_ntoa(pptr^[I]^)); Inc(I); end; WSACleanup; end; procedure TForm1.Button1Click(Sender: TObject); begin memo1.lines:=getips; end;end.
to kuki84(天道酬勤):我照你的说法做了,得到的是115.117.110.98,却不是我在网站上看到的61.243.15.16;
function Tfrm_server.GetIP : string;
type
TaPInAddr = array [0..10] of PInAddr;
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 phe = nil then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do begin
result:=StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;
end;
或者使用NMDayTime1组件也可以的.我就是这样使用的了
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,winsock;type
TaPInAddr = array[0..10] of PInAddr;
PaPInAddr = ^TaPInAddr; TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
function getIPs: tStrings;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function TForm1.getIPs: tStrings;
var
phe: PHostEnt;
pptr: PaPInAddr;
Buffer: array[0..63] of Char;
I: Integer;
GInitData: TWSAData;
begin
WSAStartup($101,GInitData);
Result := TStringList.Create;
Result.Clear;
GetHostName(Buffer,SizeOf(Buffer));
phe := GetHostByName(buffer);
if phe = nil then
begin
Exit;
end;
pPtr := PaPInAddr(phe^.h_addr_list);
I := 0;
while pPtr^[I] <> nil do
begin
Result.Add(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.lines:=getips;
end;end.