简体   繁体   English

如何获得我的计算机的外部IP地址?

[英]How to get my computer external IP address?

Lets say my IP currently is: 123.123.123.123 可以说我的IP当前是: 123.123.123.123

How can I get that string programmatically? 如何以编程方式获取该字符串?

Note: I don't want to get this IP: 192.168.0.10 , I want that IP which others can use to connect into my computer via HTTP or anything. 注意:我不想获得该IP: 192.168.0.10 ,我希望该IP可供其他人通过HTTP或其他任何方式连接到我的计算机。

NOTE: I dont want to open some web-page such as ip4.me to get the address, I want to get it with just C++. 注意:我不想打开某些网页,例如ip4.me来获取地址,我只想用C ++来获取它。

I tried to google but every suggestion was "load a page and read the IP it tells you". 我尝试使用google,但每条建议都是“加载页面并阅读其告诉您的IP”。 Is that really the only way?! 那真的是唯一的方法吗?

You can fetch it from http://api.externalip.net/ip/ or some similar services, but I'm not sure how reliable these sites are, in means of availability 您可以从http://api.externalip.net/ip/或一些类似的服务中获取它,但是我不确定这些站点的可靠性如何。

Any other way would be extremely complicated, as in general, no network equipment has api to tell external IP, and even if it had, you can not tell is there a simple xDSL router in front of you or Cisco ASA nating outbound traffic 任何其他方式都将极其复杂,因为通常,没有网络设备可以使用api来告知外部IP,即使拥有,也无法分辨出您面前是一个简单的xDSL路由器还是Cisco ASA对出站流量进行控制

My recommendation is to send a packet with the record route option. 我的建议是发送带有记录路由选项的数据包。

If you know your upstream gateway, you should be able to find a ping command that allows you to set record route, and then either stores that data in an array or something you can regex. 如果您知道上游网关,则应该能够找到可以设置记录路由的ping命令,然后将该数据存储在数组中或可以进行正则表达式的内容中。

Your WAN IP should be either record 0 or record 1, I believe. 我相信您的WAN IP应该是记录0或记录1。

What network library are you using? 您正在使用哪个网络库?

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

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