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;

LVL 3
Manuel Lopez-MichelonePhD candidateAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
2266180Commented:
you might want to have a list of sites just in case one ore more are offline ;)
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

deathman5Commented:
no, just try google
always online :P
Manuel Lopez-MichelonePhD candidateAuthor 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)

 
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.