3

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

Ohad
  • 333
  • 2
  • 4
  • 10
  • 3
    Note: there could be multiple **global** IP addresses. – John Weldon Jul 10 '10 at 21:11
  • There is nothing like global address for any software or program. What they do is get a list of ip addresses and use the first one. Do the same, use ```getaddrinfo``` and use the first address you got of the type you want – Supergamer Nov 25 '22 at 11:52

4 Answers4

4

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
  • 42,585
  • 14
  • 91
  • 146
1

Use this plain text IP url: get ip

GRUNGER
  • 486
  • 3
  • 14
1

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:

Get the IP address of the machine

Community
  • 1
  • 1
John Weldon
  • 39,849
  • 11
  • 94
  • 127
1

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
  • 75,521
  • 93
  • 229
  • 319
  • 6
    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 Jul 10 '10 at 21:27