tags:

views:

168

answers:

3

How can I get ( in C++ ) the global IP address of my computer(windows XP)?

+1  A: 

On windows you need to get the local host name and then pass that to the gethostbyname function in winsock2 which returns the associated IP addresses.

Example: http://tangentsoft.net/wskfaq/examples/ipaddr.html

A stack overflow answer for Linux:

http://stackoverflow.com/questions/212528/linux-c-get-the-ip-address-of-local-computer

John Weldon
I know how to find local IP, I want to find global IP('s).
Ohad
These methods will return external IP address (non 127.0.0.0/8)
John Weldon
Most machine hostnames will not resolve with DNS, which seems to be what you're suggesting in your first paragraph.
Thanatos
+2  A: 

You will have to make a request to a website like http://whatismyipaddress.com/ and extract the string in it that shows your IP.

bodacydo
Please note that's against their Terms of Service "You may not use a script, agent, application or otherwise query this website in an automated fashion without prior written permission.". But there's nothing to stop you hosting your own service which performs a similar task to bounce your request off.
Paul Dixon
+1  A: 

You can't.

You can determine the IP addresses on the various interfaces, and there may be more than one. These could be local area network IPs (10.0.0.0/8, 192.168.0.0/16, etc.), or they might be internet routable.

You seem to be asking "if I have 192.168.0.3, how do I get my Internet IP?" There is not function call to do this: such an IP might exist, it might not exist, there might even be more than one.

The closest you can get is to have a known computer on the Internet tell you: connect to some other machine, and ask them to send back what they think your IP address is. There are a few websites out there for this, some might even have APIs to do this.

I feel like some home-routers might be able to tell you through uPnP too, but again, this will not cover all possible cases.

Thanatos