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

IP アドレスのを作成し、HashSetそこにクライアント IP アドレスを保持できます。ハッシュ セットのルックアップにはループは必要ありません。そのContains(...)メソッドを使用するだけです。

于 2012-02-08T16:33:25.457 に答える