windows 外部 IP 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5037279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:12:33  来源:igfitidea点击:

External IP Address

c++windowsip-addressexternal

提问by Kazuma

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

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

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

嗯,又见面了。我想知道如何获得计算机的外部 IP 地址(因为有些人有路由器,所以是外部的)?

回答by user534498

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

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

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

然后写个解析器解析出来:Your IP Address Is: xxx.xxx.xxx.xxx

Voila.

瞧。

回答by AJG85

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

这是使用用户建议的一种方法......当然这仅适用于您自己的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] = '##代码##';

    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 minutesso I feel compelled to put a caveat not to run this code more often than that as well :P

仅供参考:http: //www.whatismyip.com/的所有者要求您每 5 分钟只点击一次此自动化页面因此我不得不警告不要更频繁地运行此代码:P

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

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