2012-02-12 50 views
0

我想創建一個IP隔離器。這是我創建一個TCPtable所用的模板的一部分。我試圖添加一個斷開連接功能。但是,它不會斷開連接。VB.NET IP隔離器:什麼是SetTcpEntry斷開的正確用法

Dim liste() = {"76.9.24.130" ... ... ...} 
    Dim pdwSize As Integer 
    Dim iRetVal As Integer 
    Dim i As Integer 
    Dim TcpTableRow As MIB_TCPROW 
    Dim pStructPointer As IntPtr = IntPtr.Zero 
    Dim iNumberOfStructures As Integer 
    ListView1.Items.Clear() 
    iRetVal = GetTcpTable(pStructPointer, pdwSize, 0) 
    pStructPointer = Marshal.AllocHGlobal(pdwSize) 
    iRetVal = GetTcpTable(pStructPointer, pdwSize, 0) 
    iNumberOfStructures = Math.Ceiling((pdwSize - 4)/Marshal.SizeOf(GetType(MIB_TCPROW))) 
    For i = 0 To iNumberOfStructures - 1 
     Dim pStructPointerTemp As IntPtr = New IntPtr(pStructPointer.ToInt32() + 4 + (i * Marshal.SizeOf(GetType(MIB_TCPROW)))) 
     TcpTableRow = New MIB_TCPROW() 
     With TcpTableRow 
      .dwLocalAddr = 0 
      .dwState = 0 
      .dwLocalPort = 0 
      .dwRemoteAddr = 0 
      .dwRemotePort = 0 
     End With 
     'Marshal.PtrToStructure(pStructPointerTemp, TcpTableRow) 
     TcpTableRow = CType(Marshal.PtrToStructure(pStructPointerTemp, GetType(MIB_TCPROW)), MIB_TCPROW) 
     ' Process each MIB_TCPROW here 
     'If Not ((Check1.CheckState = System.Windows.Forms.CheckState.Checked) And (GetIpFromLong(TcpTableRow.dwLocalAddr) = "0.0.0.0" Or GetIpFromLong(TcpTableRow.dwLocalAddr) = "127.0.0.1")) Then 
     If Not GetIpFromLong(TcpTableRow.dwRemoteAddr) = "127.0.0.1" And Not GetIpFromLong(TcpTableRow.dwRemoteAddr) = "0.0.0.0" Then 
      'Add the data to the ListView control 
      With TcpTableRow 
       Dim itemAdd As ListViewItem 
       itemAdd = ListView1.Items.Add(GetIpFromLong(.dwLocalAddr)) 
       itemAdd.SubItems.Add(CStr(GetTcpPortNumber(.dwLocalPort))) 
       itemAdd.SubItems.Add(GetIpFromLong(.dwRemoteAddr)) 
       itemAdd.SubItems.Add(CStr(GetTcpPortNumber(.dwRemotePort))) 
       itemAdd.SubItems.Add(GetState(.dwState)) 
      End With 
      '-------------- Kill Connection-------------- 
      If Array.IndexOf(liste, GetIpFromLong(TcpTableRow.dwRemoteAddr)) >= 0 Then 
       TcpTableRow.dwState = 12 
       SetTcpEntry(TcpTableRow) 
      End If 
     End If 
    Next 
+0

嗨,任何人有一個想法? – user670186 2012-02-20 15:49:43

回答

0

我沒能解決,但發現用CurrPorts

Shell(Application.StartupPath & "\cports /close * * " & GetIpFromLong(TcpTableRow.dwRemoteAddr) & " " & GetTcpPortNumber(TcpTableRow.dwRemotePort)) 
0

我不知道一個替代解決方案,如果這是同樣的情況或沒有,但我用的是: session = New Socket(,,,)我的連接手段通過TCP PORT23和我的問題是,我也無法得到連接關閉由於某種原因。我確實嘗試了上面的CurrPorts解決方法,但是我發現這並沒有達到我的預期。相反,我正在使用TCPClient。

 Dim TCPConnection as TCPClient 'Init TCPConnect 

    Private Sub Connect(Byval inIP) 
     Dim PiP = IPAddress.Parse(inIP) 
     Dim iplocal As New System.Net.IPEndPoint(PiP, 23) 
     Try 
      TCPsession = New TcpClient 
      TCPsession.Client.Connect(ipLocal) 
     Catch 
     'On Error Do Nothing 
     End Try 
    End Sub 

    Private Sub Disconnect() 
     TCPsession.Client.Close() 
    End Sub 

此代碼解決了我的問題,但我不確定這是否你甚至在談論什麼。

相關問題