简体   繁体   English

获取全球IP地址

[英]get global ip address

如何获得(在C ++中)我的计算机的全局 IP地址(Windows XP)?

You can't. 你不能。

You can determine the IP addresses on the various interfaces, and there may be more than one. 您可以确定各种接口上的IP地址,并且可能有多个IP地址。 These could be local area network IPs (10.0.0.0/8, 192.168.0.0/16, etc.), or they might be internet routable. 这些可能是局域网IP(10.0.0.0/8,192.168.0.0/16等),或者它们可能是互联网可路由的。

You seem to be asking "if I have 192.168.0.3, how do I get my Internet IP?" 您似乎在问“我是否有192.168.0.3,如何获取我的Internet IP?” There is not function call to do this: such an IP might exist, it might not exist, there might even be more than one. 没有函数调用来执行此操作:这样的IP可能存在,它可能不存在,甚至可能不止一个。

The closest you can get is to have a known computer on the Internet tell you: connect to some other machine, and ask them to send back what they think your IP address is. 您可以获得的最接近的是让互联网上的已知计算机告诉您:连接到其他计算机,并要求他们发回他们认为您的IP地址。 There are a few websites out there for this, some might even have APIs to do this. 有一些网站为此,有些可能甚至有API来做到这一点。

I feel like some home-routers might be able to tell you through uPnP too, but again, this will not cover all possible cases. 我觉得有些家用路由器也可能通过uPnP告诉你,但同样,这不会涵盖所有可能的情况。

使用此纯文本IP url: get ip

On windows you need to get the local host name and then pass that to the gethostbyname function in winsock2 which returns the associated IP addresses. 在Windows上,您需要获取本地主机名,然后将其传递给winsock2中的gethostbyname函数,该函数返回关联的IP地址。

Example: http://tangentsoft.net/wskfaq/examples/ipaddr.html 示例: http//tangentsoft.net/wskfaq/examples/ipaddr.html

A stack overflow answer for Linux: Linux的堆栈溢出答案:

Get the IP address of the machine 获取机器的IP地址

您必须向http://whatismyipaddress.com/等网站发出请求,并在其中提取显示您的IP的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM