2012-02-08 5 views
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アドレス)をループすると時間がかかります。手作業による検索の代わりにすばやい方法がありますか?ありがとうアレイリストに格納されているtcp-clientのIPアドレスを見つける簡単な方法。

答えて

0

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

+0

これは、最初に配列リスト(tcp-client内)とハッシュセット内の2つの異なる場所にクライアントIPを保持することを意味します。 ArrayList内のIPアドレスを検索するかどうかは、2つの異なるストレージ領域を作ることなく、直接行うことはできませんか? –

+0

@JavaneseGirl 'Dictionary 'を使って、IPアドレスで整理されたClientオブジェクトを保存することができます。リストと比較すると、欠点は列挙の順序が挿入の順序に対応しないことである。これで問題なければ、 'Dictionary'はあなたの仕事にうまくいくでしょう。 – dasblinkenlight

+0

ありがとう、私は試して成功した.. –

関連する問題