广东话域名解析成IP在C#中? [英] Cant parse domain into IP in C#?

查看:185
本文介绍了广东话域名解析成IP在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个code:

IPEndPoint serverEndPoint =新IPEndPoint(IPAddress.Parse(txtBoxIP.Text),MainForm.port);

当我在txtBoxIP(192.168.1.2),例如IP,它的伟大工程。

但是,如果我想提出一个DNS?就像我把(my.selfip.com)我得到:

  System.FormatException:指定了一个无效的IP地址。
在System.Net.IPAddress.InternalParse(字符串ipString,布尔的TryParse)
 

我怎样才能使其同时支持IP和DNS?

感谢:)

解决方案

  ip地址ip地址;
如果(!IPAddress.TryParse(txtBoxIP.Text,出ip地址))
   ip地址= Dns.GetHostEntry(txtBoxIP.Text).AddressList [0];
serverEndPoint =新IPEndPoint(ip地址,MainForm.port)
 

不要忘了错误处理。

I have this code:

IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(txtBoxIP.Text), MainForm.port);

When I have an IP in the txtBoxIP (192.168.1.2) for example, it works great.

But if i want to put a DNS? like i'm putting (my.selfip.com) i get:

System.FormatException: An invalid IP address was specified.
at System.Net.IPAddress.InternalParse(String ipString, Boolean tryParse)

How can i make it support both IP and DNS ?

Thanks :)

解决方案

IPAddress ipAddress;
if (!IPAddress.TryParse (txtBoxIP.Text, out ipAddress))
   ipAddress = Dns.GetHostEntry (txtBoxIP.Text).AddressList[0];
serverEndPoint = new IPEndPoint(ipAddress, MainForm.port)

Don't forget the error handling.

这篇关于广东话域名解析成IP在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆