在 C# 中确定公共 IP 并与主机名进行比较
我有一个 C# 程序,我想从我在地下室设置的服务器获取新闻源。我还想设置这个程序,以便它可以在本地运行。为此,我认为我需要将 dyndns.biz 主机名的解析 ip 与路由器的公共 ip 进行比较(我的服务器上有动态 ip 和更新主机名 ip 的客户端),从而确定主机名是否需要使用或服务器的本地 IP (192.168.0.100) 或主机名。我已经有了连接到 ftp 服务器的代码,假设我可以正确使用主机名和本地化 IP。
编辑:无论如何,总而言之,因为我意识到这可能看起来不像一个问题,我如何确定 a) 主机名的解析 IP 和 b) C# 应用程序中路由器的公共 IP
I have a program in C# that I want to get a news feed from a server I setup in my basement. I also want to setup this program so it can work locally. To do this I THINK I need to compare the resolved ip of my dyndns.biz hostname to my router's public ip (I have dynamic ip and a client on my server updating the ip of the hostname) and thus determine if the hostname needs to be used or the local ip of the server (192.168.0.100) or the hostname. I already have code to connect to the ftp server assuming I can get the right usage of the hostname versus the localized IP.
Edit: Anyways, in summary because I realized this might not look like a question, how can I determine a) the resolved IP of the hostname and b) the public IP of my router in a C# app
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你把这件事想得太复杂了。
只需打开您的
hosts
文件(位于C:\Windows\System32\drivers\etc
中)并添加您的 dyndns 主机名,将其路由到环回。这样,您就不必向最终应用程序添加任何解决方法代码来阻止它甚至询问您的 DNS 或路由器:You're overcomplicating this.
Just open your
hosts
file (found inC:\Windows\System32\drivers\etc
) and add your dyndns hostname routing it to loopback. That way you don't have to add any workaround code to your final application to prevent it from even asking your DNS or router:要获取主机名的 IP 地址,请使用以下代码:
要获取路由器或本地网络的公共 IP 地址,您需要与可以告诉您该部分的外部系统通信,不幸的是我不知道是否可以有任何这样的系统可以免费使用并且可以通过程序轻松使用。
To obtain the IP address of a hostname, use the following code:
To obtain your public IP address of your router or local network, you need to talk to an outside system that can tell you that part, and unfortunately I don't know if there is any such system that is free to use as well as easy to use from a program.