1

Possible Duplicate:
Stable way of retrieving the external IP for a host behind a NAT

Well, hello again. I was wondering how do i get the External IP Address (External since some people haves router) of the computer?

Community
  • 1
  • 1
Kazuma
  • 1,371
  • 6
  • 19
  • 33
  • What platform? What libraries? What? – AJG85 Feb 18 '11 at 03:24
  • 2
    How many routers do you go up to find the external IP address. That's imposable to tell automatically. Thus there is no generic way to do this. You can ask the router but this will be router specific. – Martin York Feb 18 '11 at 03:30
  • Well, anyway is fine. I need to get it by any way. – Kazuma Feb 18 '11 at 03:30
  • OK, that's the language. What platform? It matters. – Graeme Perrow Feb 18 '11 at 03:30
  • The platform is Windows. Ops, i missed tag. Editing it. :/ – Kazuma Feb 18 '11 at 03:32
  • That would be a programming language. You need to clarify the question for a figurative example: "I'm writing a simply console app for Windows and using winsock library, is there a way for me to get external ip addresses easily or is there another, preferably open source, C++ socket or networking library that can do this?" FYI, the answer to that would probably be not really this is not as trivial as you might think :P – AJG85 Feb 18 '11 at 03:36

2 Answers2

3

Write a program and use HTTP protocol to connect to some external websites such as this one: http://www.whatismyip.com/

Then write a parser to parse out: Your IP Address Is: xxx.xxx.xxx.xxx

Voila.

user534498
  • 3,926
  • 5
  • 27
  • 52
1

Here's one way of using user's suggestion ... of course this only works for your own ip which you could also determine by opening the command prompt and running ipconfig /all

#include <windows.h>
#include <wininet.h>
#include <iostream>

#pragma comment(lib, "wininet")

int main(int argc, char* argv[])
{
    HINTERNET hInternet, hFile;
    DWORD rSize;
    char buffer[32];

    hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    hFile = InternetOpenUrl(hInternet, "http://automation.whatismyip.com/n09230945.asp", NULL, 0, INTERNET_FLAG_RELOAD, 0);
    InternetReadFile(hFile, &buffer, sizeof(buffer), &rSize);
    buffer[rSize] = '\0';

    InternetCloseHandle(hFile);
    InternetCloseHandle(hInternet);

    std::cout << "Your IP Address: " << buffer << "\n";
    system("pause");
    return 0;
}   

FYI: The owner's of http://www.whatismyip.com/ request that you only hit this automation page once every 5 minutes so I feel compelled to put a caveat not to run this code more often than that as well :P

Note: http://automation.whatismyip.com/n09230945.asp is the newest address for the automation file. the 5 minute / 300 second rule is still in place.

Community
  • 1
  • 1
AJG85
  • 15,849
  • 13
  • 42
  • 50