无法在C#中将域解析为IP? [英] Can't parse domain into IP in C#?

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

问题描述

我有此代码:

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

例如,当我在txtBoxIP(192.168.1.2)中有一个IP时,它会很好用.

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

但是,如果我想放置DNS?就像我放(my.selfip.com)一样,我得到了:

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)

我如何使其同时支持IP和DNS?

How can I make it support both IP and DNS ?

推荐答案

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

别忘了错误处理.

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

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