Can't parse a domain in IP in C #?

I have this code:

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

For example, if I have an IP address in txtBoxIP (192.168.1.2), it works fine.

But if I want to put DNS? as I put (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?

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

Do not forget about error handling.

+8
source

DNS list to IP list

 IPHostEntry nameToIpAddress; nameToIpAddress = Dns.GetHostEntry("HostName"); foreach (IPAddress address in nameToIpAddress.AddressList) Console.WriteLine(address.ToString()); 

Then you can use the IP address in the AddressList.

Here is a great article.

+2
source

The DNS name is not an IP address. See Dns.GetHostEntry() for DNS resolution.

Edited to add: Here is what I did:

 public static IPEndPoint CreateEndpoint( string hostNameOrAddress , int port ) { IPAddress addr ; bool gotAddr = IPAddress.TryParse( hostNameOrAddress , out addr ) ; if ( !gotAddr ) { IPHostEntry dnsInfo = Dns.GetHostEntry( hostNameOrAddress ) ; addr = dnsInfo.AddressList.First() ; } IPEndPoint instance = new IPEndPoint( addr , port ) ; return instance ; } 
+2
source
 var input = txtBoxIP.Text; IPAddress ip; // TryParse returns true when IP is parsed successfully if (!IPAddress.TryParse (input, out ip)) // in case user input is not an IP, assume it a hostname ip = Dns.GetHostEntry (input).AddressList [0]; // you may use the first one // note that you'll also want to handle input errors // such as invalid strings that are neither IPs nor valid domains, // as well as hosts that couldn't be resolved var serverEndPoint = new IPEndPoint (ip, MainForm.port); 
+2
source

You will need to search for the hostname IP address yourself:

 string addrText = "www.example.com"; IPAddress[] addresslist = Dns.GetHostAddresses(addrText); foreach (IPAddress theaddress in addresslist) { Console.WriteLine(theaddress.ToString()); } 

Edit

To talk about the differences between them (BTW uses some C # features, which can be in 3.5 and above):

 bool isDomain = false; foreach(char c in addrText.ToCharArray()) { if (char.IsLetter(c)){ isDomain = true; break; } if (isDomain) // lookup IP here else // parse IP here 
+1
source

Note: No, this is not déjà vu, this answer is the same as I presented on another duplicate question ... I wanted the author to know something different, so the best way I found to add it again here is as simply links to other answers are not approved when the stack overflows.

I have a very neat extension method for this!

Whereas, IPV6 can be returned as the first address in the list of addresses returned by the DNS class, and allows you to "approve" IPV6 or IPV4 for the result. Here is a fully documented class (only with the appropriate method for this case for brevity reasons):

 using System; using System.Linq; using System.Net; using System.Net.Sockets; /// <summary> /// Basic helper methods around networking objects (IPAddress, IpEndPoint, Socket, etc.) /// </summary> public static class NetworkingExtensions { /// <summary> /// Converts a string representing a host name or address to its <see cref="IPAddress"/> representation, /// optionally opting to return a IpV6 address (defaults to IpV4) /// </summary> /// <param name="hostNameOrAddress">Host name or address to convert into an <see cref="IPAddress"/></param> /// <param name="favorIpV6">When <code>true</code> will return an IpV6 address whenever available, otherwise /// returns an IpV4 address instead.</param> /// <returns>The <see cref="IPAddress"/> represented by <paramref name="hostNameOrAddress"/> in either IpV4 or /// IpV6 (when available) format depending on <paramref name="favorIpV6"/></returns> public static IPAddress ToIPAddress(this string hostNameOrAddress, bool favorIpV6=false) { var favoredFamily = favorIpV6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork; var addrs = Dns.GetHostAddresses(hostNameOrAddress); return addrs.FirstOrDefault(addr => addr.AddressFamily == favoredFamily) ?? addrs.FirstOrDefault(); } } 

Remember to put this class in the namespace! :-)

Now you can simply do this:

 var server = "http://simpax.com.br".ToIPAddress(); var blog = "http://simpax.codax.com.br".ToIPAddress(); var google = "google.com.br".ToIPAddress(); var ipv6Google = "google.com.br".ToIPAddress(true); // if available will be an IPV6 
0
source

All Articles