Private Function validateport(ByVal port As Int32) As Boolean
Try
Dim soc As New Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
soc.Bind(New IPEndPoint(Dns.Resolve(Dns.GetHostName).AddressList(0).GetHashCode, port))
soc = Nothing
Return True
Catch ex As Exception
Return False
End Try
End Function
#Region "Shadows"
Public Shadows Function Add(ByVal value As Client, Optional ByVal Connect As Boolean = False) As Integer
Return MyBase.Add(value)
RaiseEvent NewClient(value)
If Connect Then
value.Connect()
End If
End Function
Default Public Shadows Property Item(ByVal index As Integer) As Client
Get
Return DirectCast(MyBase.Item(index), Client)
End Get
Set(ByVal Value As Client)
MyBase.Item(index) = Value
End Set
End Property
Public Shadows Function GetRange(ByVal index As Integer, ByVal count As Integer) As SocketClients
Return DirectCast(MyBase.GetRange(index, count), SocketClients)
End Function
Public Shadows Function Contains(ByVal item As Client) As Boolean
Return MyBase.Contains(item)
End Function
Public Shadows Function IndexOf(ByVal value As Client) As Integer
Return MyBase.IndexOf(value)
End Function
Public Shadows Function IndexOf(ByVal value As Client, ByVal startIndex As Integer) As Integer
Return MyBase.IndexOf(value, startIndex)
End Function
Public Shadows Function IndexOf(ByVal value As Client, ByVal startIndex As Integer, ByVal count As Integer) As Integer
Return MyBase.IndexOf(value, startIndex, count)
End Function
Public Shadows Sub Insert(ByVal index As Integer, ByVal value As Client)
MyBase.Insert(index, value)
End Sub
Public Shadows Sub InsertRange(ByVal index As Integer, ByVal c As SocketClients)
MyBase.InsertRange(index, c)
End Sub
Public Shadows Sub AddRange(ByVal c As SocketClients)
MyBase.AddRange(c)
End Sub
Public Shadows Function LastIndexOf(ByVal value As Client) As Integer
Return MyBase.LastIndexOf(value)
End Function
Public Shadows Function LastIndexOf(ByVal value As Client, ByVal startIndex As Integer) As Integer
Return MyBase.LastIndexOf(value, startIndex)
End Function
Public Shadows Function LastIndexOf(ByVal value As Client, ByVal startIndex As Integer, ByVal count As Integer) As Integer
Return MyBase.LastIndexOf(value, startIndex, count)
End Function
Public Shadows Sub Remove(ByVal obj As Client)
MyBase.Remove(obj)
End Sub
Public Shadows Sub SetRange(ByVal index As Integer, ByVal c As SocketClients)
MyBase.SetRange(index, c)
End Sub
Public Shadows Function Clone() As SocketClients
Return DirectCast(MyBase.Clone, SocketClients)
End Function
#End Region
Public Class Client
Private _IPAddress As IPAddress
Private _RemEP As IPEndPoint
Private _LocEP As IPEndPoint
Private _soc As Net.Sockets.Socket
Private _UserName As String
Private _State As ConnectedState
Public Enum ConnectedState
NotConnected = 0
Connecting = 1
Connected = 2
Errors = 3
End Enum
Private regexIP As New System.Text.RegularExpressions.Regex("^(25[0-5]|2[0-4]" & _
"[0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4]" & _
"[0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")
Public Sub New(ByVal strIP As String, ByVal intPort As Int32)
Try
If regexIP.IsMatch(strIP) Then
_IPAddress = New IPAddress(IPAddress.Parse(strIP).GetHashCode)
_RemEP = New IPEndPoint(_IPAddress, SocketClients.RequestPort)
_LocEP = New IPEndPoint(Dns.Resolve(Dns.GetHostName).AddressList(0), intPort)
_State = ConnectedState.NotConnected
Else
_State = ConnectedState.Errors
Throw New System.ArgumentException(strIP & " is not a valid IP Format.", "strIP")
End If
Catch ex As ArgumentException
_State = ConnectedState.Errors
Throw New System.ArgumentException(strIP & " is not a valid IP Format.", "strIP", ex)
End Try
End Sub
Public Sub New(ByVal lngIP As Long, ByVal intPort As Int32)
Try
_IPAddress = New IPAddress(lngIP)
_RemEP = New IPEndPoint(_IPAddress, SocketClients.RequestPort)
_LocEP = New IPEndPoint(Dns.Resolve(Dns.GetHostName).AddressList(0), intPort)
_State = ConnectedState.NotConnected
Catch ex As ArgumentException
_State = ConnectedState.Errors
Throw New ArgumentException(lngIP & " is not a valid IP Format.", "lngIP", ex)
End Try
End Sub
Public Sub CreateSocket(ByVal addressFamily As Net.Sockets.AddressFamily, ByVal socketType As Net.Sockets.SocketType, ByVal protocolType As Net.Sockets.ProtocolType)
_soc = New System.Net.Sockets.Socket(addressFamily, socketType, protocolType)
End Sub
#Region "Connection State"
Public Sub Connect()
Try
_State = ConnectedState.Connecting
If _soc Is Nothing Then
CreateSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
End If
_soc.Bind(_LocEP)
_State = ConnectedState.Connected
Catch ex As Exception
_State = ConnectedState.Errors
End Try
End Sub
Public Sub Disconnect()
'TODO:
End Sub
#End Region
Public ReadOnly Property IPAddress() As IPAddress
Get
Return _IPAddress
End Get
End Property
Public ReadOnly Property RemoteEndPoint() As IPEndPoint
Get
Return _RemEP
End Get
End Property
Public ReadOnly Property UserName() As String
Get
Return _UserName
End Get
End Property
Public ReadOnly Property State() As ConnectedState
Get
Return _State
End Get
End Property
End Class
End Class