【问题标题】:External IP Address [duplicate]外部 IP 地址 [重复]
【发布时间】:2011-06-29 13:35:31
【问题描述】:

可能重复:
Stable way of retrieving the external IP for a host behind a NAT

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

【问题讨论】:

  • 什么平台?有哪些图书馆?什么?
  • 你上多少个路由器才能找到外部IP地址。这是不可能自动判断的。因此,没有通用的方法可以做到这一点。您可以询问路由器,但这将是特定于路由器的。
  • 好吧,无论如何都很好。我需要以任何方式得到它。
  • 好的,这就是语言。什么平台?这很重要。
  • 平台是Windows。操作,我错过了标签。编辑它。 ://

标签: c++ windows ip-address external


【解决方案1】:

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

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

瞧。

【讨论】:

  • 好主意...让我扩展一下!
  • 在一个无关的笔记上,有谁知道whatismyip.org发生了什么?
【解决方案2】:

这是使用用户建议的一种方式...当然,这仅适用于您自己的 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 秒规则仍然有效。

【讨论】:

    猜你喜欢
    • 2013-08-04
    • 1970-01-01
    • 2011-07-29
    • 2021-11-20
    • 2013-04-03
    • 2016-03-10
    • 2021-07-06
    • 2022-08-14
    相关资源
    最近更新 更多