[Lazarus] How to find out the public IP in code?

Mark Morgan Lloyd markMLl.lazarus at telemetry.co.uk
Sat Nov 7 13:07:47 CET 2015

Bo Berglund wrote:
> 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?

Avoid relying on this in the general case: you can usually use 
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 

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.

Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]

More information about the Lazarus mailing list