• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 661
  • Last Modified:

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:='WSANotInitialised';
    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;

0
lopem
Asked:
lopem
  • 4
1 Solution
 
2266180Commented:
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
0
 
2266180Commented:
ok, since it's pretty easy, here is the code:

var s:string; i:integer;
begin
  s:=IdHTTP1.Get('http://www.whatismyip.com/');
  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
0
 
2266180Commented:
you might want to have a list of sites just in case one ore more are offline ;)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
deathman5Commented:
no, just try google
always online :P
0
 
lopemAuthor Commented:
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)

 
0
 
2266180Commented:
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
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now