Solved

Programmatically detecting my router's IP address

Posted on 2004-10-04
13
684 Views
Last Modified: 2010-04-05
Hello...
  How can I programmatically obtain my router's IP address? I have 2 computers on this router, and when I do a :

localIP:= TcpClient1.LocalHostAddr;    

(as suggested by someone in an old question of mine), it just returns the local router-assigned IP address - 192.168.x.xxx.

I need to be able to get the main IP address of my router, ie. 24.xxx.xxx.xxx

Thanks
   Shawn
0
Comment
Question by:aztec
  • 5
  • 2
  • 2
  • +3
13 Comments
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12223635
I have a LinkSys and what I had to do what use an http component to grab the linksys status page, then parse out the router's IP address. It's fairly easy when you use an http component, and you know the local address of your router, and the page to request.
0
 

Author Comment

by:aztec
ID: 12223645
Thanks but I need something more "universal" for my app. Not all my users are going to be using Linksys routers.

Thanks
   Shawn


0
 
LVL 12

Accepted Solution

by:
Ivanov_G earned 50 total points
ID: 12224248
one dirty solution - create a PHP file:

<?
  $ip = getenv("REMOTE_ADDR");
  echo $ip;
?>

upload this page somewhere on the web and when you open it with TidHTTP component - you can see you external IP. This is "universal", but the disadvange is that it requires Internet connection...
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12224322
How about using TRACERT?  

Not sure whether there is a Delphi component that does that, but if the worst comes to the worst you can Exec the DOS command to a text file, then parse that.  The Router public address is going to be the hop after the internal address.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12225392
It's no use...

Your client connects to your server through the router. For this connection, your client doesn't first go from Router to your ISP then back to your router to finally go to your server. Nope, no way. Why would he? Your client connects directly to your server through the router. That outside IP number never gets involved into this equation.

If you do want your "outside" IP number to appear in the server, you must connect to your server through the outside world. Thus, don't connect to it's local IP number or it's machine name. Set up the router that it will send all incoming network traffic to your server. Then connect your client to the outside (24.xxx.xxx.xxx) IP number to force a connection to your server from the outside.

Compare it like walking from your kitchen to your living room. You could walk through the inside of your house and get there, without ever looking outside. Or you go through the kitchen door to the outside, walk around your house and enter through the front door. It's the latter thing that you're trying to do...
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12225797
If you do

tracert   borland.com

(any domain will do)

this will give you a report of all the hops along the way, including the router's public ip address.  If this report is parsed: you know what the Gateway IP address is (you can get this from the Registry), it's going to be the entry after that.
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12225807
Sorry, that should be

tracert   borland.com >c:\some directory\tracert.log

0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12226808
Well, if he uses: "tracert server" or "tracert 10.0.1.20" or "tracert 192.168.1.101" then his tracert log will look like this:
------------------------------------------------
Tracing route to <server> over a maximum of 30 hops

  1   <10 ms   <10 ms   <10 ms  <server>

Trace complete.
------------------------------------------------
With <server> being whatever he provided as servername, of course... And no, it doesn't look useful either.
The problem with the router is that it has two sides. The inside and the outside. As long as you make connections from the inside to the inside, it is like walking in your house. You never get to see anything from the outside.

If you want to see the outside IP number, Ivanov_G just gave a good solution. A simple webpage that will return your IP number to you. If you make it appear as if your client is connecting to your server from the outside, then you'll have to tell the client to connect to the outside IP number, and tell the router to direct all incoming connections to the server that you've set up.

There is NO other option.
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12228685
When I type in the following line (verbatim):-

tracert   borland.com

I get a listing which shows quite clearly my gateway public IP address. It is, in fact, the first entry.  It is in square brackets, the content of which is easily parsed, if redirected into a file.

Ivanov_G's solution is fine.  It depends on the mindset of the asker.  If the asker is more at home with DOS and parsing techniques than with PHP, my method will maybe have more appeal.
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12228767
One other point.

Doing a tracert on a domain will clearly not work if there are problems with DNS.  In practice, I would use a known external dotted IP address rather than a domain name for reliability reasons.
0
 
LVL 1

Expert Comment

by:tommyliu
ID: 12273756
Dear Shawn,

   I want to know your router is support http admin mode, that means can I use web browser to admin the router ???

Tommy
0
 

Author Comment

by:aztec
ID: 12279016
Thanks Ivanov (sorry for the slow response). I think this will work well. Where do I find the TidHTTP component?

Thanks
   Shawn

0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12284283
It is part from Indy components. If you are with Delphi 7, you will have them integrated. If not - here is the download address:
http://www.indyproject.org/download/Files/Indy9.html
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
delphi prevent click fast 2 220
code issue 8 131
delphi parse string to params 3 123
Delphi...Split view - idea? 1 69
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question