none
VB.net API SetTcpEntry 的請教 RRS feed

  • 問題

  • Public Class Form1
      Private Const ERROR_BUFFER_OVERFLOW As Short = 111
      Private Const ERROR_INVALID_PARAMETER As Short = 87
      Private Const ERROR_NO_DATA As Short = 232
      Private Const ERROR_NOT_SUPPORTED As Short = 50
      Private Const ERROR_SUCCESS As Short = 0
      Private Const MIB_TCP_STATE_CLOSED As Short = 1
      Private Const MIB_TCP_STATE_LISTEN As Short = 2
      Private Const MIB_TCP_STATE_SYN_SENT As Short = 3
      Private Const MIB_TCP_STATE_SYN_RCVD As Short = 4
      Private Const MIB_TCP_STATE_ESTAB As Short = 5
      Private Const MIB_TCP_STATE_FIN_WAIT1 As Short = 6
      Private Const MIB_TCP_STATE_FIN_WAIT2 As Short = 7
      Private Const MIB_TCP_STATE_CLOSE_WAIT As Short = 8
      Private Const MIB_TCP_STATE_CLOSING As Short = 9
      Private Const MIB_TCP_STATE_LAST_ACK As Short = 10
      Private Const MIB_TCP_STATE_TIME_WAIT As Short = 11
      Private Const MIB_TCP_STATE_DELETE_TCB As Short = 12
      Declare Function GetTcpTable Lib "Iphlpapi" (ByVal pTcpTable As IntPtr, ByRef pdwSize As Integer, ByVal bOrder As Boolean) As Integer
      Declare Function SetTcpEntry Lib "IPhlpAPI" (ByRef pTcpRow As MIB_TCPROW) As Integer
    
      Public Structure MIB_TCPROW
        Public dwState As Integer
        Public dwLocalAddr As Integer
        Public dwLocalPort As Integer
        Public dwRemoteAddr As Integer
        Public dwRemotePort As Integer
      End Structure
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        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
          TcpTableRow = CType(Marshal.PtrToStructure(pStructPointerTemp, GetType(MIB_TCPROW)), MIB_TCPROW)
          If Not ((CheckBox1.CheckState = System.Windows.Forms.CheckState.Checked) And (GetIpFromLong(TcpTableRow.dwLocalAddr) = "0.0.0.0" Or GetIpFromLong(TcpTableRow.dwLocalAddr) = "127.0.0.1")) Then
            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
          End If
        Next
      End Sub
    End Class
    

    如何用SetTcpEntry刪除TCP連接

    我在網路都找不到

    能不能請大大堤供範例

    提外問題   要如何偵測 ListView 上選取的一比資料 然後刪除那筆資料?

    例如 選取ListView.Items 裡面的 ListViewItems.Text

    2011年2月28日 上午 02:42

解答

所有回覆

  • 看過這一篇嗎 ?

    關於 SetTcpEntry 的問題


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。
    2011年2月28日 上午 02:59
    版主
  •     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim a As MIB_TCPROW() = getTCPConnectionTable()
            For i As Integer = 0 To a.Length - 1
                Dim __row As MIB_TCPROW = CType(a(i), MIB_TCPROW)
                Dim __localPort As Integer = __row.dwLocalPort
                Dim __remoetIP As String = GetIpFromLong(__row.dwRemoteAddr)
                If __remoetIP = "xx.xx.xx.xx" Then ' >> 這裏想要針對某些IP中斷其連線
                    If __row.dwState = TCPState.MIB_TCP_STATE_ESTAB Then ' >> 根據 MSDN 說明, 似乎只有 established 狀態的 Connection 才可以變更狀態
                        __row.dwState = TCPState.MIB_TCP_STATE_DELETE_TCB ' >> 同上, 似乎也只能設定為 DELETE_TCB
                        MsgBox(SetTcpEntry(__row).ToString()) ' >> 這裏顯示出 SetTcpEntry 的結果, 卻永遠都是 87, "Specified port is not in state to be closed down"
                    End If
                End If
            Next
            End
        End Sub

    謝謝 Bill 大大 提供的參考 但

    SetTcpEntry 的參數只有一個而已   要如何 傳MIB_TCP_STATE_DELETE_TCB 和 IP 到參數?

    2011年2月28日 上午 04:28
  •             If __remoetIP = "xx.xx.xx.xx" Then ' >> 這裏想要針對某些IP中斷其連線
                    If __row.dwState = TCPState.MIB_TCP_STATE_ESTAB Then ' >> 根據 MSDN 說明, 似乎只有 established 狀態的 Connection 才可以變更狀態
                        __row.dwState = TCPState.MIB_TCP_STATE_DELETE_TCB ' >> 同上, 似乎也只能設定為 DELETE_TCB
                        MsgBox(SetTcpEntry(__row).ToString()) ' >> 這裏顯示出 SetTcpEntry 的結果, 卻永遠都是 87, "Specified port is not in state to be closed down"
                    End If
                End If
    
    
    

    這段程式,您有試過嗎?SetTcpEntry傳進去是結構,dwRemoteAddr不能改,dwState設定MIB_TCP_STATE_DELETE_TCB,跑起來有問題嗎...

    • 已標示為解答 Just Lin 2011年2月28日 上午 07:48
    2011年2月28日 上午 05:27
  • 2011年2月28日 上午 07:50