Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't parse domain into IP in C#?

Tags:

c#

.net

ip

dns

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 ?

like image 699
Danpe Avatar asked Jun 27 '11 20:06

Danpe


1 Answers

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.

like image 75
agent-j Avatar answered Sep 28 '22 02:09

agent-j