外部IP地址

发布于 2024-10-18 03:24:14 字数 263 浏览 0 评论 0原文

可能的重复:
检索外部的稳定方式NAT 后面的主机的 IP

好吧,你好。我想知道如何获取计算机的外部 IP 地址(外部,因为有些人有路由器)?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生未歇 2024-10-25 03:24:14

编写一个程序并使用 HTTP 协议连接到一些外部网站,例如:
http://www.whatismyip.com/

然后写一个解析器解析出来:
您的 IP 地址是:xxx.xxx.xxx.xxx

瞧。

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.

肤浅与狂妄 2024-10-25 03:24:14

这是使用用户建议的一种方法...当然,这仅适用于您自己的 IP,您也可以通过打开命令提示符并运行 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;
}   

仅供参考:http://www.whatismyip.com/ 请求您仅每 5 分钟一次 所以我觉得有必要提出警告,不要更频繁地运行此代码:P

注意: http ://automation.whatismyip.com/n09230945.asp 是自动化文件的最新地址。 5 分/300 秒的规则仍然有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文