topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Sunday December 15, 2024, 12:13 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Programatically determining your IP Address...  (Read 24031 times)

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Programatically determining your IP Address...
« on: June 27, 2008, 11:40 AM »
So, I'm working on a little application that has a few random utilities for myself... I'm using CodeGear RAD Studio and wondering if someone could explain the logical process of how to determine my IP Address (locally and externally) programatically... Is there some DLL I could call or some system command that I could use... I know there are commands I could use in the command prompt for windows but I was wondering if there was any other way to do it... Help is appreciated much :)
CodeByter.com - http://www.codebyter.com
« Last Edit: June 27, 2008, 11:47 AM by Codebyte »

Eóin

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 1,401
    • View Profile
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #1 on: June 27, 2008, 12:40 PM »
Coincidentally I read a snippet about this recently. For local IP address check out GetAdaptersInfo API function.

For the external IP I think you need to look into using a webservice which reports its. For example http://checkip.dyndns.org/.

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #2 on: June 27, 2008, 05:59 PM »
What Eóin said :) - external IP address is a bit of a mess. You need to depend on an external resource, which may or may not be available... so be careful about depending on that functionality. Ie., if you need the external IP address for a server application or whatever, you should offer auto-detection as a feature, but still let the user specify it manually.
- carpe noctem

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #3 on: June 27, 2008, 06:45 PM »
so, here's a question... is implementing this "auto-detection" feature really difficult? Not too good with networking... Thanks for the reply's also :)

EDIT: Because of my inexperience with networking, my original idea went a little something like this:

http://ip-address.cc/ shows you your external IP. I took a look at the page's source code and it seems as though your external IP address is always contained at the beginning of line 120. So, I know this might sound like an "odd" idea, but I was gonna have the executable grab the source code for the site listed above and do some simple string manipulation in order to grab the external IP address... Please tell me if I sound crazy?
CodeByter.com - http://www.codebyter.com
« Last Edit: June 27, 2008, 06:52 PM by Codebyte »

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #4 on: June 27, 2008, 07:06 PM »
Don't go for line numbers - read up on regular expressions :)

You might want to find a few of those services, and try them in round-robin if the first one fails.
- carpe noctem

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #5 on: June 27, 2008, 07:40 PM »
kk, that will give me somewhere to start... Thanks alot f0dder! Also, is that auto-detection thing a tough implementation? I wouldnt know how to go about doing it...
CodeByter.com - http://www.codebyter.com

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #6 on: June 27, 2008, 07:47 PM »
Doesn't have to be *that* tough - as long as you have libraries that handle HTTP connections and regular expressions.

Basically, fetch a page (for instance http://checkip.dyndns.org/ or http://ip-address.cc/), try to match the Regular Expression you have designed for scraping the IP, and if it succeeds - you have the IP. If you want to code everything by hand, it will be complex. But if you have HTTP and RegEx libraries, it's a pretty quick job.
- carpe noctem

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #7 on: June 27, 2008, 08:20 PM »
Now, only left to do is figure out how to grab a web page's source code programatically.... Once I get this done, ill upload a sample executable to see if the String routine will work correctly with different IP's...
CodeByter.com - http://www.codebyter.com

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #8 on: June 30, 2008, 12:02 AM »
k, here is a test run... This application should grab the source from http://checkip.dyndns.org/ and use a simple string manipulation to find your external IP address... Please post and let me know the results.

If this doesnt work for everyone thats tries it, im going to set up an algorithm to grab what looks like the ip from a few several sources and perform string manipulation on them all. At the end, ill compare results and come up with a score sheet to make an estimated guess at the most accurate IP address... For now, I simply need feedback on this one...

http://www.codebyter...grams/externalip.exe
CodeByter.com - http://www.codebyter.com

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,914
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #9 on: June 30, 2008, 12:09 AM »
works well here.  :up:

lanux128

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 6,277
    • View Profile
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #10 on: June 30, 2008, 12:15 AM »
works for me too.. i checked the external IP address with eToolz and they matched!. :up:

wreckedcarzz

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 1,626
    • View Profile
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #11 on: June 30, 2008, 02:11 AM »
Works well here :)

Question: Would it be possible to have it give your your IP when the form loads, instead of having to click the button? I could definitely use this program for when I host game servers behind my router, and having it show up automatically is just that much more convenient.

cthorpe

  • Discount Coordinator
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 738
  • c++thorpe
    • View Profile
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #12 on: June 30, 2008, 03:40 AM »
k, here is a test run... This application should grab the source from http://checkip.dyndns.org/ and use a simple string manipulation to find your external IP address... Please post and let me know the results.

Worked for me as well.

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #13 on: June 30, 2008, 08:47 AM »
Wreckedcarzz, Im working on an executable for you right now... Just woke up lol! Is there anything else you would like added to it?
CodeByter.com - http://www.codebyter.com

wr975

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 369
    • View Profile
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #14 on: June 30, 2008, 08:59 AM »
Now, only left to do is figure out how to grab a web page's source code programatically.... Once I get this done, ill upload a sample executable to see if the String routine will work correctly with different IP's...

Try this URL: http://www.netikus.net/show_ip.html

Just the IP. Nothing more (no source code to search).

Autohotkey example:
UrlDownloadToFile,http://www.netikus.net/show_ip.html,%a_temp%\myip.txt
Fileread,IP,%a_temp%\myip.txt
msgbox,your IP is %ip%

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #15 on: June 30, 2008, 09:19 AM »
Good find! I was looking for one of those sites that would supply only the IP address! Anyways, WreckedCarzz: Here is your executable! :)

http://www.codebyter...grams/externalip.exe
CodeByter.com - http://www.codebyter.com
« Last Edit: June 30, 2008, 09:21 AM by Codebyte »

mediaguycouk

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 247
    • View Profile
    • Mediaguy
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #16 on: June 30, 2008, 09:36 AM »
Whatismyip.com also have a single line IP page for automation

http://www.whatismyi...mation/n09230945.asp
Learning C# - Graham Robinson

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #17 on: June 30, 2008, 10:08 AM »
Whatismyip.com also have a single line IP page for automation

http://www.whatismyi...mation/n09230945.asp
-mediaguycouk (June 30, 2008, 09:36 AM)
Times out here...
- carpe noctem

jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #18 on: June 30, 2008, 10:15 AM »
Times out here...
That's because they know you're a hacker and you'd manage to DOS them through that page :P
:) jk.. It's working here.

cranioscopical

  • Friend of the Site
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,776
    • View Profile
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #19 on: June 30, 2008, 10:22 AM »
Please post and let me know the results
-Codebyte

www.whatismyip.com agrees with your externalip.exe

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #20 on: June 30, 2008, 12:14 PM »
Good stuff! Thanks guys!
CodeByter.com - http://www.codebyter.com

wreckedcarzz

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 1,626
    • View Profile
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #21 on: July 01, 2008, 11:29 PM »
Thanks CodeByte! Works great :) :up:

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: Programatically determining your IP Address...
« Reply #22 on: July 02, 2008, 12:16 AM »
np, enjoy :) let me know if you need something else
CodeByter.com - http://www.codebyter.com