I would like to be able to find out the public IP address of the Pi in a Lazarus program.
How can this be accomplished?
It is either connected to a wired or WiFi network router. in my browser I can go to http://checkip.dyndns.com/ and it will print a message with my IP address.
But how is this done in code?
Mailing lists / lazarus / 2015-11
How to find out the public IP in code?
6 messages · 5 people ·
- Ffecske fecske 2
- AAradeonas 1
- BBo Berglund 1
- MMark Morgan Lloyd 1
- Ssilvioprog 1
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Bo Berglund wrote 7 lines
Avoid relying on this in the general case: you can usually use 0.0.0.0 to represent "this host". The problem is that a machine might have multiple physical Ethernet (or other) adapters, and in addition might have aliases set up, and in the general case it's not easy to look at the list of IP addresses and determine which one you want.
http://forum.lazarus.freepascal.org/index.php?topic=20556.0 looks like a good starting point, although it's oriented towards Windows. If you're really looking for "/the/" address then I think you might end up parsing the routing table (left as an exercise :-) and finding which local IP address talks to the default gateway... which itself gets messy since there might be multiple prioritised gateways. And then there's IP6 to consider.
Word of advise: don't try arithmetic operations on IP addresses, and in particular don't speculate on "next hops" etc. based on what you know about your local network.
forexample
http://wiki.lazarus.freepascal.org/Internet_Tools#Get_external_IP_address
Mark Morgan Lloyd wrote 40 lines
Avoid relying on this in the general case: you can usually use 0.0.0.0 to represent "this…
or see this
http://forum.lazarus.freepascal.org/index.php/topic,17506.msg110917.html#msg110917
Mark Morgan Lloyd wrote 40 lines
fecske fecske wrote
If you just want to get your external IP using some service like dyndns, you should use pure FCL:
=== begin code ===
uses fphttpclient, RegExpr;
procedure TForm1.Button1Click(Sender: TObject);
begin
with TRegExpr.Create('[0-9.]+') do try
if Exec(TFPHTTPClient.SimpleGet('http://checkip.dyndns.org')) then
ShowMessage(Match[0])
finally Free; end;
end;=== end code ==
However, if you can use other service like echoip.com, just:
=== begin code ===
uses fphttpclient;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(TFPHTTPClient.SimpleGet('http://echoip.com/'));
end;=== end code ===
HTH
So much clean answer Slivio.
Regards, Ara
