sevenhalo
Well-known member
I have a namespace called "NetworkComponents" of various tools I'm using for network awareness. One of the classes in Network is called Host. It's a custom object I'm using to resolve a Machine Name or an IP Address to a manageable object.
When I construct an object of type NetworkComponents.Host, I'm attempting to validate through DNS as well. The problem I'm running into is, if a Net.Sockets.SocketException occurs, how can I throw an exception back to the method creating the object to let it know that DNS didn't resolve? This is what I'm basically attempting to do:
VB.NET:
Namespace NetworkComponents
Class Host
Private _Host As System.Net.IPHostEntry '--The main element being used
Private _LocalHost As Boolean = False '--Constructed image from localmachine
Private _IPArray() As System.Net.IPAddress '--Array of IP Addresses associated with _Host
#Region "Constructor"
Public Sub New(Optional ByVal strHost As String = "")
'''Will accept either Machine Name or IP of the Host to construct
Try
'A blank value indicates that they want Local as the host
If strHost <> "" Then
Dim RE As New System.Text.RegularExpressions.Regex("((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)")
If RE.IsMatch(strHost) Then
'strHost is an IP Address in string notation
_Host = System.Net.Dns.Resolve(strHost)
Else
'strHost is a name of a Machine
_Host = System.Net.Dns.Resolve(strHost)
End If
Else
'Construct based on local computer
_Host = System.Net.Dns.Resolve(System.Net.Dns.GetHostName)
_LocalHost = True
End If
Catch ex As Net.Sockets.SocketException
'DNS Exception, name/IP did not resolve
Catch ex As ArgumentException
'Something slipped, whatever they passed didn't look like either notations
End Try
End Sub
#End Region
VB.NET:
Private Sub btnTestCmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestCmd.Click
Try
Dim test As New NetworkComponents.Host(Me.txtHostBox.Text.Trim)
Catch ex As NetworkComponents.DNXException
'Notify User of invalid Host
End Try
End Sub