开发者

Determining public IP in C# and comparing to a hostname

开发者 https://www.devze.com 2023-03-06 01:02 出处:网络
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

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 hostn开发者_Go百科ame 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


You're overcomplicating this. Just open your hosts file (found in C:\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:

127.0.0.1 yourhostname.dyndns.biz


To obtain the IP address of a hostname, use the following code:

IPAddress[] addresses = System.Net.Dns.GetHostAddresses("www.cnn.com");

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消