Manuel Lopez-Michelone
asked on
Real IP address...
Hi experts,
I am trying to get the real Internet IP address thru Delphi. I am behind a router (or proxy) so my code gives me only the local Internet-IP-Address of my machine. I am using this code, from about.com (see below), but it doesn't give me the real IP address. Any ideas?
uses Winsock;
function GetIPFromHost
(var HostName, IPaddr, WSAErr: string): Boolean;
type
Name = array[0..100] of Char;
PName = ^Name;
var
HEnt: pHostEnt;
HName: PName;
WSAData: TWSAData;
i: Integer;
begin
Result := False;
if WSAStartup($0101, WSAData) <> 0 then begin
WSAErr := 'Winsock is not responding."';
Exit;
end;
IPaddr := '';
New(HName);
if GetHostName(HName^, SizeOf(Name)) = 0 then
begin
HostName := StrPas(HName^);
HEnt := GetHostByName(HName^);
for i := 0 to HEnt^.h_length - 1 do
IPaddr :=
Concat(IPaddr,
IntToStr(Ord(HEnt^.h_addr_ list^[i])) + '.');
SetLength(IPaddr, Length(IPaddr) - 1);
Result := True;
end
else begin
case WSAGetLastError of
WSANOTINITIALISED:WSAErr:= 'WSANotIni tialised';
WSAENETDOWN :WSAErr:='WSAENetDown';
WSAEINPROGRESS :WSAErr:='WSAEInProgress';
end;
end;
Dispose(HName);
WSACleanup;
end;
procedure TForm1.Button1Click(Sender : TObject);
var
Host, IP, Err: string;
begin
if GetIPFromHost(Host, IP, Err) then begin
Edit1.Text := Host;
Edit2.Text := IP;
end
else
MessageDlg(Err, mtError, [mbOk], 0);
end;
I am trying to get the real Internet IP address thru Delphi. I am behind a router (or proxy) so my code gives me only the local Internet-IP-Address of my machine. I am using this code, from about.com (see below), but it doesn't give me the real IP address. Any ideas?
uses Winsock;
function GetIPFromHost
(var HostName, IPaddr, WSAErr: string): Boolean;
type
Name = array[0..100] of Char;
PName = ^Name;
var
HEnt: pHostEnt;
HName: PName;
WSAData: TWSAData;
i: Integer;
begin
Result := False;
if WSAStartup($0101, WSAData) <> 0 then begin
WSAErr := 'Winsock is not responding."';
Exit;
end;
IPaddr := '';
New(HName);
if GetHostName(HName^, SizeOf(Name)) = 0 then
begin
HostName := StrPas(HName^);
HEnt := GetHostByName(HName^);
for i := 0 to HEnt^.h_length - 1 do
IPaddr :=
Concat(IPaddr,
IntToStr(Ord(HEnt^.h_addr_
SetLength(IPaddr, Length(IPaddr) - 1);
Result := True;
end
else begin
case WSAGetLastError of
WSANOTINITIALISED:WSAErr:=
WSAENETDOWN :WSAErr:='WSAENetDown';
WSAEINPROGRESS :WSAErr:='WSAEInProgress';
end;
end;
Dispose(HName);
WSACleanup;
end;
procedure TForm1.Button1Click(Sender
var
Host, IP, Err: string;
begin
if GetIPFromHost(Host, IP, Err) then begin
Edit1.Text := Host;
Edit2.Text := IP;
end
else
MessageDlg(Err, mtError, [mbOk], 0);
end;
probably the best solution will be to make a request to some site (like http://www.whatismyip.com/) and parse the result. this will show the real ip address even if you are on a LAN that has only one real ip address or you are getting out through a proxy
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you might want to have a list of sites just in case one ore more are offline ;)
no, just try google
always online :P
always online :P
ASKER
Ciuly,
A simple solution to a difficult problem. I am using Delphi 5 and I found only the NMHTTP component. It has also a GET method, but I am getting an error message... It doesn't accept
s := NMHTTP1.Get('http://www.whatismyip.com'); //<-- this line gives me an incompatibe types error. Any idea?
regards
Manuel Lopez (lopem)
A simple solution to a difficult problem. I am using Delphi 5 and I found only the NMHTTP component. It has also a GET method, but I am getting an error message... It doesn't accept
s := NMHTTP1.Get('http://www.whatismyip.com'); //<-- this line gives me an incompatibe types error. Any idea?
regards
Manuel Lopez (lopem)
well, you can always download indy and install it (it's opensource ;) ) from here: http://www.indyproject.org/Sockets/Download/Borland.en.iwp (Use version 9 as it is the one shipped with delphi 7; it should work fine on delphi 5 as well. if it doesn't, uninstall and use version 8 (this one is shipped with delphi 6 if I remember correctly)
i never worked with that component (I azctually jumped from delphi 3 to delphi 6) but I did a little search on the net and found the following code: http://immortals.fake.hu/delphiportal/modules.php?name=News&file=article&sid=1978
so for this case the code should look like:
var s:string; i:integer;
begin
NMHTTP1.Get('http://www.whatismyip.com/');
s:=NMHTTP1.Body.Text;
delete(s,1,pos('Your IP Is',s)+10);
i:=1;
while s[i] in ['0'..'9','.'] do
inc(i);
delete(s,i,length(s));
showmessage(s);
end;
cheers
i never worked with that component (I azctually jumped from delphi 3 to delphi 6) but I did a little search on the net and found the following code: http://immortals.fake.hu/delphiportal/modules.php?name=News&file=article&sid=1978
so for this case the code should look like:
var s:string; i:integer;
begin
NMHTTP1.Get('http://www.whatismyip.com/');
s:=NMHTTP1.Body.Text;
delete(s,1,pos('Your IP Is',s)+10);
i:=1;
while s[i] in ['0'..'9','.'] do
inc(i);
delete(s,i,length(s));
showmessage(s);
end;
cheers