tags:

views:

538

answers:

8

How can I get the same IP Address I get when I go to "http://www.whatsmyip.org/" using C++ and winsock library?

I know how to get the "127.0.0.1" and the router IP "192.168.1.103"... But when I got to "http://www.whatsmyip.org/" I get "65.55.105.132"...

How can I accomplish this?

+14  A: 

In a general way - you can't. You could open an http connection to whatmyip and parse the result. Or as Justin suggests in the comments, use http://www.whatismyip.com/automation/n09230945.asp and save on parsing and bandwidth. But in the end, that's only going to give you the IP of the NAT / proxy / whatever that's between you and whatsmyip. That address is only connected to your computer through forwarding.

There isn't a general way to retrieve your router's internet facing IP address (that's the 65.55.105.132 IP you are seeing.) Even if you could, there's no reason that there couldn't be more layers of NATing getting in the way.

Eclipse
+1, dont know why you were downvoted. your answer is correct.
Robin Day
http://www.whatismyip.com/automation/n09230945.asp is a better page to query for this
Justin
@Justin - thanks! I hadn't seen that before.
Eclipse
Huh, me neither, that's really good to know.
GMan
@Eclipse whatismyip actually prefers this way because of bandwidth. It's a win win
Justin
+3  A: 

You can't.

http://www.whatsmyip.org is telling you the IP address it sees when you talk to it. It could be seeing yours, or a proxy server, or a NAT box, or anything else. There's no way for you to know.

What are you trying to accomplish?

John Saunders
+1  A: 

Semi-Duplicate: Try looking at this SO question it is for C# but the same applies for C++

Justin
A: 

Well, there is no trick really. The reason www.whatsmyip.com can tell your "outside" IP address is because they receive a request that has been NATed (Network Address Translated) by your router and so they see your public IP address. That is something that you can't really get at an arbitrary internal host on your network - not unless you have a server on the outside that you could query or have a mechanism to ask your router what IP address it uses when making public internet requests..

Miky Dinescu
And in many cases, his public IP address is likely to change, possibly from moment to moment.
John Saunders
A: 

As others have said, there may not be an easy / great solution. An (albeit hacky) solution might be to shell out to the 'tracert' system call. Ex:

tracert google.com

This call will return a list of all routers in between your PC and google.com (or whatever external site.) You can then iterate through the results, taking the first node that isn't a private IP address range. (I.e., not 192.168.., etc.)

Granted, this is by no means an elegant solution. However, it might give you the info you need. As a test, you can simply run the above from a command line and see if it's helpful.

Eric Pi
@Eric Pi: How often will that change?
John Saunders
@John: Some portions of the overall tracert route will likely change very often. (Depending on which routers/servers happen to handle a given packet.) However, the earliest few hops should be reasonably stable-- changing only when the user changes their internal network / router setup.
Eric Pi
@And if he's going to bake knowledge this into a program, then when the internal network / router setup changes, his program will break, unless his "network guys" think to tell the developers that they're going to make harmless changes to the network.
John Saunders
+1  A: 

There is no way to accomplish this without using something that is outside of your network.

The address returned is the first public ip address between your computer and the outside world. 10., 192.168., 172.[16-31].* ip address are "private" and should not be forwarded intact to any non-private ip address.

At some point, private ip addresses must be linked to a public ip address. The first public ip address in the path from your computer to the whatsmyip.com site is what is displayed on that page. The trick is that your computer might have a public ip, or there might be some number of hops to computers/switches/routers that have private ip's until there is eventually a public ip. There is no way to know what that number of hops is, and depending on the network the route can change from one request to another and there migth be a different number of hops each time (unlikely for a home network, but more likely in a corporate network).

The only way to get the first public ip address is to send a packet to an address that is public, and trace the route that it took. Again...that route can (and does) change on a complex network.

If you have a specific problem that you are trying to solve, there might be a better answer.

semiuseless
+1  A: 

As others have said, you need a third party on the network such as whatismyip.com to tell you your IP address. I just wanted to add, you shouldn't depend on whatismyip.com, instead you should consider setting up your own, as services like this tend to come and go or change who may connect to them (see this example for why that may happen).

You can easily set up a service to do this just for your application for free, say, based on Google AppEngine. The requisite request handler might look something like:

from google.appengine.ext import webapp

class RemoteAddressHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write(self.request.remote_addr)
David Wilson
And, again, that may very well not be a permanent address, even if he's reachable via an inbound connection.
John Saunders
A: 

Wow that was a very quick reply. Thanks guys for the answers now I know better.

I am currently building a C++ HTTP server for learning purpose. I accomplish to forward the IP address I get when I got to "whatismyip.org" to my private ip "192.168.1.3" making any external call to be handle by my server. So pretty much I created a simple C++ server. However I am using something called No-IP DUC to translate "yellowyackets.no-ip.org" to my public IP so that people don't have to memorize my public IP. But because the IP changes I have to keep track to whats is my new IP so that people can keep connecting to me... so I have to go to "whatsmyip.org" to know my new IP. Then I wanted to create a program that would actually tell me thats my new IP.

I dunno if what I just said make sense but... I am brand new to servers... Like I said this is for learning purpose.

Thanks again. and good luck to everyone.

Y_Y