Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2015-11

How to find out the public IP in code?

6 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bo Berglund

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?

Bo Berglund Developer in Sweden

Reply by email

Mark Morgan Lloyd
to Bo Berglund
Bo Berglund wrote 7 lines
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 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.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

fecske fecske
to Mark Morgan Lloyd

forexample

http://wiki.lazarus.freepascal.org/Internet_Tools#Get_external_IP_address

Mark Morgan Lloyd wrote 40 lines
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 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. -- Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues] -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

fecske fecske
to Mark Morgan Lloyd: 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
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 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. -- Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues] -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

silvioprog
to fecske fecske
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

Silvio Cl?cio -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151107/87fdcea3/attachment.html>

Reply by email