我有这个代码:
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(txtBoxIP.Text), MainForm.port);
例如,当我在txtBoxIP(192.168.1.2)中有IP时,效果很好.
但是,如果我想放一个DNS?就像我放(my.selfip.com)我得到的:
System.FormatException: An invalid IP address was specified.
at System.Net.IPAddress.InternalParse(String ipString, Boolean tryParse)
Run Code Online (Sandbox Code Playgroud)
如何使其支持IP和DNS?
IPAddress ipAddress;
if (!IPAddress.TryParse (txtBoxIP.Text, out ipAddress))
ipAddress = Dns.GetHostEntry (txtBoxIP.Text).AddressList[0];
serverEndPoint = new IPEndPoint(ipAddress, MainForm.port)
Run Code Online (Sandbox Code Playgroud)
不要忘记错误处理.