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

' 如果超过 1000 个客户端连接到服务器,则循环查找数组列表(tcp 客户端)中的对象(IP 地址),需要很长时间。有没有一种快速的方法来代替手动搜索工作。谢谢

4

1 回答 1

0

您可以创建一个HashSetIP 地址,并将客户端 IP 地址保留在那里。在哈希集中查找不需要循环:您只需使用它的Contains(...)方法。

于 2012-02-08T16:33:25.457 回答