Recent

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

evoshroom

  • Full Member
  • ***
  • Posts: 157
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.
« Last Edit: January 31, 2013, 09:39:09 am by evoshroom »

ttomas

  • Full Member
  • ***
  • Posts: 245
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #1 on: July 11, 2012, 09:26:39 am »
Only your router/gateway know your external IP. You can use any httpclient to ask your router or universal solution to ask any free MyIP service or http response and parse result. If you have your own web server, then it is easy to write simple php, python, java, pascal page to return your IP. Some free MyIP sites.
http://www.whatismyip.com/
http://whatismyipaddress.com/
http://myip.dk/
or some Restful service
http://api.hostip.info/get_json.php
« Last Edit: July 11, 2012, 09:36:48 am by ttomas »

JD

  • Hero Member
  • *****
  • Posts: 1848
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

evoshroom

  • Full Member
  • ***
  • Posts: 157
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #3 on: July 11, 2012, 03:01:39 pm »
Here's how you can do it in Indy

http://stackoverflow.com/questions/7015483/how-to-get-external-public-ip-in-delphi?rq=1

Indy is not truly cross-platform, which is why I asked in regard to Synapse and Lnet.

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #4 on: July 11, 2012, 03:21:49 pm »
Here's how you can do it in Indy

http://stackoverflow.com/questions/7015483/how-to-get-external-public-ip-in-delphi?rq=1

Indy is not truly cross-platform, which is why I asked in regard to Synapse and Lnet.

I'm using Indy 10 on Linux (Ubuntu & Mint) and Windows. But I've never tried it on a Mac.

JD
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

evoshroom

  • Full Member
  • ***
  • Posts: 157
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #5 on: July 11, 2012, 03:23:28 pm »
Here's how you can do it in Indy

http://stackoverflow.com/questions/7015483/how-to-get-external-public-ip-in-delphi?rq=1

Indy is not truly cross-platform, which is why I asked in regard to Synapse and Lnet.

I'm using Indy 10 on Linux (Ubuntu & Mint) and Windows. But I've never tried it on a Mac.

JD

I have, as that's my main development environment.  It is buggy.

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #6 on: July 11, 2012, 03:44:47 pm »
Here's how you can do it in Indy

http://stackoverflow.com/questions/7015483/how-to-get-external-public-ip-in-delphi?rq=1

Indy is not truly cross-platform, which is why I asked in regard to Synapse and Lnet.

I'm using Indy 10 on Linux (Ubuntu & Mint) and Windows. But I've never tried it on a Mac.

JD

I have, as that's my main development environment.  It is buggy.

I see. Sorry about that. I can't help you there. There might be something in FPC source network units that can do the job. For instance inside the file fpsock.pp (...\source\packages\fcl-net\src\fpsock.pp), there is a TSocketStream class that has the structure shown below:

Code: [Select]
  TSocketStream = class(THandleStream)
  private
    FOnDisconnect: TNotifyEvent;
    function GetLocalAddress: TSockAddr;
    function GetPeerAddress: TSockAddr;
  protected
    procedure Disconnected; virtual;
  public
    destructor Destroy; override;
    function Read(var Buffer; Count: LongInt): LongInt; override;
    function Write(const Buffer; Count: LongInt): LongInt; override;

    property LocalAddress: TSockAddr read GetLocalAddress;
    property PeerAddress: TSockAddr read GetPeerAddress;
    property OnDisconnect: TNotifyEvent read FOnDisconnect write FOnDisconnect;
  end;

There are many other classes that may be able to do the job but I've never used them because I've always worked with Indy.

JD
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #7 on: July 11, 2012, 05:43:18 pm »
That Indy technique can perfectly be done with fcl-web:
Code: [Select]
{$mode objfpc}{$H+}

uses
  fphttpclient;

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

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #8 on: July 11, 2012, 07:25:33 pm »
That Indy technique can perfectly be done with fcl-web:
Code: [Select]
{$mode objfpc}{$H+}

uses
  fphttpclient;

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

Thanks a lot. I'll be sure to try that. The more the merrier!!!  :D
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #9 on: July 12, 2012, 02:59:32 am »
Only your router/gateway know your external IP.
If you run ipconfig command in the command prompt, it will tell your external IP. I don't know how it fetches that info, but it's surely not using any webservice for it  :P

evoshroom

  • Full Member
  • ***
  • Posts: 157
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #10 on: July 12, 2012, 03:08:58 am »
That Indy technique can perfectly be done with fcl-web:
Code: [Select]
{$mode objfpc}{$H+}

uses
  fphttpclient;

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

Thanks a lot. I'll be sure to try that. The more the merrier!!!  :D

I have tried it.  At least on Mac it isn't so great.  It did work, however, I get a spinning beach ball of death (app freeze) for a full 35 seconds when I execute it.  Maybe if isolated in a thread or something it could be okay if you don't need to use the IP for 35 seconds or so after opening your app.  Also, it may work a bit better when the HTTP component of Synapse or Lnet is used instead of fpHTTPclient, which I'd be interested to see.

evoshroom

  • Full Member
  • ***
  • Posts: 157
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #11 on: July 12, 2012, 04:44:01 am »
Only your router/gateway know your external IP.
If you run ipconfig command in the command prompt, it will tell your external IP. I don't know how it fetches that info, but it's surely not using any webservice for it  :P

Do you (or anyone else) have any premade code for fetching the IP address on Windows with ipconfig?  Combining this with similar unix terminal fetch of the ip might also do the trick and would be all executable locally.  However, are you sure ipconfig just doesn't fetch the local IP rather than the external one?
« Last Edit: July 12, 2012, 05:02:42 am by evoshroom »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #12 on: July 12, 2012, 05:09:46 am »
Quote
I have tried it.  At least on Mac it isn't so great.  It did work, however, I get a spinning beach ball of death (app freeze) for a full 35 seconds when I execute it.  Maybe if isolated in a thread or something it could be okay if you don't need to use the IP for 35 seconds or so after opening your app.  Also, it may work a bit better when the HTTP component of Synapse or Lnet is used instead of fpHTTPclient, which I'd be interested to see.
It's not the library problem IMO, using any library would be the same since the base, lowest functionality used is the same (RTL sockets unit). You can try using another website like http://ifconfig.me/ip.

@User137:
ipconfig doesn't show external ip, external ip is inaccessible from the computer, since it's the router who managed it (and big chance you don't have access to it since it belongs to your ISP).

evoshroom

  • Full Member
  • ***
  • Posts: 157
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #13 on: July 12, 2012, 05:22:19 am »
Quote
I have tried it.  At least on Mac it isn't so great.  It did work, however, I get a spinning beach ball of death (app freeze) for a full 35 seconds when I execute it.  Maybe if isolated in a thread or something it could be okay if you don't need to use the IP for 35 seconds or so after opening your app.  Also, it may work a bit better when the HTTP component of Synapse or Lnet is used instead of fpHTTPclient, which I'd be interested to see.
It's not the library problem IMO, using any library would be the same since the base, lowest functionality used is the same (RTL sockets unit). You can try using another website like http://ifconfig.me/ip.


You are correct; it was the website.  http://ifconfig.me/ip worked very quickly with the same code.

bambamns

  • Full Member
  • ***
  • Posts: 223
Re: How Do I Find My External IP Address (using Synapse, Lnet or FPC)
« Reply #14 on: July 12, 2012, 05:29:32 am »
Hi,

You ca use http://ifconfig.me/ip with LNet and HTTPClient with :
Code: [Select]
function TForm1.HTTPClientInput(ASocket: TLHTTPClientSocket; ABuffer: pchar;
  ASize: integer): integer;
var
  oldLength: dword;
  HTTPBuffer: string;
begin
  oldLength := Length(HTTPBuffer);
  setlength(HTTPBuffer,oldLength + ASize);
  move(ABuffer^,HTTPBuffer[oldLength + 1], ASize);
  MemoHTML.Text := HTTPBuffer;
  MemoHTML.SelStart := Length(HTTPBuffer);
  Result := aSize; // tell the http buffer we read it all
end;         
« Last Edit: July 12, 2012, 05:50:42 am by bambamns »
Lazarus 1.8.4 + FPC 2.6.4 x86 (rebuild) and Lazarus 2.0, Windows 7 x64, unless otherwise specified

 

TinyPortal © 2005-2018