Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux / C++: Get Internet IP Address (not local computer's IP)

How can I programmatically get the Internet IP address?

1) If the computer is directly connected to the Internet using a USB modem.

2) If the computer is connected to the internet via another computer or a modem/router.

I there a way to do both?

P.S. This link gives exactly the Internet IP, but how can I use it in my program?


2 Answers

You need to talk to an external server. Issuing HTTP requests to sites like http://checkip.dyndns.org or http://www.whatismyip.com will do the trick.

To do the HTTP request, you can for example use libcurl.

like image 81
Krumelur Avatar answered Oct 23 '25 11:10

Krumelur


If you want to access a web page via c++, go for CurlPP. Use it to download the whatismyip-page you already found and you're done.

like image 22
Malte Clasen Avatar answered Oct 23 '25 12:10

Malte Clasen