Private _clientTCPList As ArrayList = ArrayList.Synchronized(New ArrayList())
' get tcp client
Dim clientTCP As TcpClient = serverTCP.EndAcceptTcpClient(ar)
' get new ip address from tcp client
Dim newClientIPAddress as IPAddress = TryCast(clientTCP.Client.RemoteEndPoint, IPEndPoint).Address
' find new ip-address in tcp-client array list, if ip-address isn't found then add new
tcp-client to last index of aray list, but if founded then replace old tcp-client in array list with new tcp-client object.
Dim i As Integer = 0
Dim clientIPAddressFound As Boolean = False
SyncLock _clientTCPList
For Each t As TcpClient In _clientTCPList
If t.Client IsNot Nothing Then
Dim oldClientIPAddress As IPAddress = TryCast(t.Client.RemoteEndPoint, IPEndPoint).Address
If oldClientIPAddress IsNot Nothing Then
If Object.Equals(oldClientIPAddress, newClientIPAddress) Then
clientIPAddressFound = True
Exit For
End If
End If
End If
i += 1
Next
End SyncLock
' If the more then 1000 client connected to a server,looping to find the objects(ip address) inside array list(tcp clients), takes a long time. Is there a quick methode to replace the manual search of work. Thanks
You can create a HashSet
of IP addresses, and keep client IP addresses there. Lookup in hash set does not require a loop: you simply use its Contains(...)
method.