如何获得(在C ++中)我的计算机的全局 IP地址(Windows XP)?
You can't.
You can determine the IP addresses on the various interfaces, and there may be more than one. These could be local area network IPs (10.0.0.0/8, 192.168.0.0/16, etc.), or they might be internet routable.
You seem to be asking "if I have 192.168.0.3, how do I get my 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.
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. There are a few websites out there for this, some might even have APIs to do this.
I feel like some home-routers might be able to tell you through uPnP too, but again, this will not cover all possible cases.
使用此纯文本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.
Example: http://tangentsoft.net/wskfaq/examples/ipaddr.html
A stack overflow answer for Linux:
您必须向http://whatismyipaddress.com/
等网站发出请求,并在其中提取显示您的IP的字符串。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.