开发者

External IP Address [duplicate]

开发者 https://www.devze.com 2023-02-11 14:01 出处:网络
This question already has answers here: 开发者_Go百科 Closed 11 years ago. Possible Duplicate: Stable way of retrieving the external IP for a host behind a NAT
This question already has answers here: 开发者_Go百科 Closed 11 years ago.

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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消