Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get global ip address

Tags:

c++

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

like image 685
Ohad Avatar asked Oct 29 '25 03:10

Ohad


2 Answers

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.

like image 104
Thanatos Avatar answered Oct 30 '25 18:10

Thanatos


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

like image 20
bodacydo Avatar answered Oct 30 '25 19:10

bodacydo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!