0
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

Javanese Girl
  • 337
  • 2
  • 6
  • 14

1 Answers1

0

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • This means I keep the client ip in two different places, first on the array list(inside tcp-client) and the second in hashset. Whether to search ip-address in the ArrayList can not be done directly, without having to make two different storage areas? – Javanese Girl Feb 09 '12 at 00:54
  • @JavaneseGirl You could use a `Dictionary` to store Client objects organized by their IP addresses. Compared to list, the drawback is that the order of enumeration will not correspond to the order of insertions. If this is OK with you, the `Dictionary` will work fine for your task. – Sergey Kalinichenko Feb 09 '12 at 01:20