Recent

Author Topic: [SOLVED] How Do I Find My External IP Address (using Synapse, Lnet or FPC)  (Read 30785 times)

User137

  • Hero Member
  • *****
  • Posts: 1790
    • Nxpascal home
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #15 on: July 12, 2012, 05:41:49 am »
However, are you sure ipconfig just doesn't fetch the local IP rather than the external one?
It is external IP, same one that other people use to connect to my local server softwares. Took 2 screens to confirm, just masked part of the end, but they are identical.

DirkS

  • Sr. Member
  • ****
  • Posts: 251
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #16 on: July 12, 2012, 08:06:12 am »
If your PC is behind a router ipconfig will normally give you the local IP address, so it's not a reliable method.


Gr.
Dirk.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #17 on: July 12, 2012, 10:28:31 am »
However, are you sure ipconfig just doesn't fetch the local IP rather than the external one?
It is external IP, same one that other people use to connect to my local server softwares. Took 2 screens to confirm, just masked part of the end, but they are identical.
Ipconfig gives you the the IP address on your NIC/network card.
Dirks is right: if you have a router (or a combination cable modem+router, ADSL+router etc) between you and the internet, you will connect your computer on an internal network (LAN) to that router and you will have a different NIC IP address than your external IP address - they're on different networks.

However, if you're using a bridge to connect to the internet (such as in certain cable modems), this bridge is "invisible" on the IP layer and the ISP's DHCP server will assign your PC an IP address... the external IP address. So user137's screenshot is probably taken from that situation.

No idea how this is done with analog modems...

Summary: NIC IP addresses can be but often are not external IP addresses. Agreed that the only possibilities are:
  • asking your router for the IP address - which way will differ per router
  • asking some external service for the IP address
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

evoshroom

  • Full Member
  • ***
  • Posts: 157
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #18 on: July 12, 2012, 10:33:06 am »
So assuming we are going to use this method (since we can't pull it from the local system):

Code: [Select]
uses
  fphttpclient;

begin
  with TFPHTTPClient.Create(nil) do
    try
      ShowMessage(Get('http://automation.whatismyip.com/n09230945.asp'));
    finally
      Free;
    end;
end.

Does anyone have better code that uses something like this in a visual program, but is closer to a best practice?  The idea of pulling a string from web is fine, but this specific code:

1) Will cause your program to delay if the website is laggy.
2) May cause your program to error if the website is down (?)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8103
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #19 on: July 12, 2012, 10:54:51 am »
Quote
Does anyone have better code that uses something like this in a visual program, but is closer to a best practice?
Code? I don't think so. But you could have your own external server whose reliability you could maintain yourself.

evoshroom

  • Full Member
  • ***
  • Posts: 157
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #20 on: July 12, 2012, 10:56:10 am »
Quote
Does anyone have better code that uses something like this in a visual program, but is closer to a best practice?
Code? I don't think so. But you could have your own external server whose reliability you could maintain yourself.

Well, I mentioned this before, but wouldn't threading this make some sense?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8103
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #21 on: July 12, 2012, 11:07:05 am »
Quote
Well, I mentioned this before, but wouldn't threading this make some sense?
Could be, in a GUI program this might help.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #22 on: July 12, 2012, 11:08:26 am »
Using threading, you could also query another server if the first one doesn't respond, etc.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

jostudio

  • Newbie
  • Posts: 1
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #23 on: January 08, 2013, 05:42:51 pm »
using synapse, send a http request to checkip.dyndns.org, get IP address from response.

code example:

uses
  Classes,SysUtils,synaip,httpsend,synautil; 

...

//get external internet IP addresses (if the computer is in LAN)
function GetExternalIp:String;
var
  HTTPGetResult: boolean;
  HTTPSender: THTTPSend;
  URL:String;
  Content : TStringList;
  s:String;
begin
    result:='';
    URL:='http://checkip.dyndns.org';
    HTTPSender:=THTTPSend.Create;
    Content :=TStringList.Create;
    try
      HTTPGetResult:=HTTPSender.HTTPMethod('GET', URL);
      if HTTPSender.Resultcode=200 then
      begin
        Content.LoadFromStream(HTTPSender.Document);
        if Content.Count>0 then
        begin
          s:=Content.Strings[0];
          synautil.Fetch(s,'Current IP Address:');
          s:=synautil.Fetch(s,'<');
          s:=trim(s);
          if IsIp(s) then result:=s;
        end;
      end;
    finally
      Content.Free;
      HTTPSender.Free;
    end;
end;               



BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #24 on: January 27, 2013, 12:22:27 pm »
FYI, modified last post by jostudio and added it here:
http://wiki.lazarus.freepascal.org/fphttpclient

Thanks, jostudio!
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

reth

  • Newbie
  • Posts: 1
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #25 on: January 31, 2013, 09:21:17 am »
Anyone know of a cross-platform method to find your external IP address using Synapse, Lnet or basic free pascal?  I've been Googling and searching the forums and haven't found anything.

You can use this site Ip-details.com to find the external ip address.

evoshroom

  • Full Member
  • ***
  • Posts: 157
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #26 on: January 31, 2013, 09:37:55 am »
Anyone know of a cross-platform method to find your external IP address using Synapse, Lnet or basic free pascal?  I've been Googling and searching the forums and haven't found anything.

You can use this site Ip-details.com to find the external ip address.

You can use many websites of course.  Those websites which merely return a string, such as http://checkip.dyndns.org provided in the example, are simply more elegant as you don't have to then parse the HTML to get it to output the desired information and less information in total is transferred, which is slightly more efficient.

It's a nice bit of sample code.

I just realized I'm the one who made this topic originally, when first getting into Lazarus socket libs.  Adding "[SOLVED]" to the topic. :)
« Last Edit: January 31, 2013, 09:41:04 am by evoshroom »

geno

  • Full Member
  • ***
  • Posts: 198
Re: [SOLVED] How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #27 on: January 31, 2013, 06:02:40 pm »
Here is another very simple example, I ran across this IP website in an article about tor / torify:

Code: [Select]
Procedure TForm1.Button1Click(Sender: TObject);
var
    hprocess: TProcess;
Begin
   hProcess := TProcess.Create(nil);
   hProcess.Executable := '/usr/bin/curl';
   hprocess.Parameters.add('ifconfig.me');
   hProcess.Options := hProcess.Options + [poWaitOnExit, poUsePipes];
   hProcess.Execute;
   memo1.Lines.LoadFromStream(hprocess.Output);
   hProcess.Free;
 end;

If run from the command line (using curl or whatever fits your fancy), the website ifconfig.me returns just the ip address - no parsing required. Navigate in a web browser and you can get your remote host info as well.

Just my two cents!!  :)

  regards,
      geno
version:      Lazarus 1.9   FPC 3.0.4
   
widget set:  x-86_64-linux-gtk 2
OS:             ArchLinux/ Xfce 4.12