C# Socket issue
-
I am having difficulty filtering IP Addresses using IPAddress.TryParse() I am trying to get upto speed with .NET programming, running MSVS 2005 .NET on Vista Home Pro. System Config Notebook one ethernet + built-in WiFi. On form load I request Internet Host Info and aquire numerous addresses, and add them to the listbox for selection later. The problem some do not reflect a true IP address are varied lengths some look like partial mac adresses etc, and are invalid for speific reasons hense the need to filter them. Having stepped through debug I am now assuming the reason IPAddress.TryParse() is not filtering them is because the values are valid based on the AddressList[] static members: Any, Broadcast, IPv6Any ... the list continues. Identifying IPv6Loopback as being the problem for the following scenario because IPv6Loopback contains {::1} For simplicity, with no ethernet connection & WiFi disabled; I have 2 items in the listbox: ::1 (I regard as a problem) 127.0.0.1 (Loopback no problem) The code: string strIP = null; IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName()); if (hostEntry.AddressList.Length > 0) { foreach (IPAddress ip in hostEntry.AddressList) { strIP = ip.ToString(); IPAddress tmpAddress; if (IPAddress.TryParse(strIP, out tmpAddress)) { socketListBox.Items.Add(strIP); } } } If my assumtion is correct how can I filter the items which cause phyical issues. Regards
dotman1
-
I am having difficulty filtering IP Addresses using IPAddress.TryParse() I am trying to get upto speed with .NET programming, running MSVS 2005 .NET on Vista Home Pro. System Config Notebook one ethernet + built-in WiFi. On form load I request Internet Host Info and aquire numerous addresses, and add them to the listbox for selection later. The problem some do not reflect a true IP address are varied lengths some look like partial mac adresses etc, and are invalid for speific reasons hense the need to filter them. Having stepped through debug I am now assuming the reason IPAddress.TryParse() is not filtering them is because the values are valid based on the AddressList[] static members: Any, Broadcast, IPv6Any ... the list continues. Identifying IPv6Loopback as being the problem for the following scenario because IPv6Loopback contains {::1} For simplicity, with no ethernet connection & WiFi disabled; I have 2 items in the listbox: ::1 (I regard as a problem) 127.0.0.1 (Loopback no problem) The code: string strIP = null; IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName()); if (hostEntry.AddressList.Length > 0) { foreach (IPAddress ip in hostEntry.AddressList) { strIP = ip.ToString(); IPAddress tmpAddress; if (IPAddress.TryParse(strIP, out tmpAddress)) { socketListBox.Items.Add(strIP); } } } If my assumtion is correct how can I filter the items which cause phyical issues. Regards
dotman1
use Dns.Resolve instead og Dns.GetHostEntry method however the method is obsolete :( but it works fine good luck :)
-
use Dns.Resolve instead og Dns.GetHostEntry method however the method is obsolete :( but it works fine good luck :)
-
use Dns.Resolve instead og Dns.GetHostEntry method however the method is obsolete :( but it works fine good luck :)
-
Yeah it works fine now but it'll probably get blasted in a future version of the framework. If a method is marked as obsolete it's a warning that you shouldn't use it if you want your code to be easily upgraded in the future.