VB.NET IP断开器:SetTcpEntry断开连接的正确用法是什么

发布于 2025-01-04 23:41:28 字数 2320 浏览 1 评论 0原文

我正在尝试创建一个 IP 断开器。这是我创建 TCPtable 的模板的一部分。我试图添加一个断开连接的功能。但是,它不会断开连接。

    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

I am trying to create an IP disconnector. This is part from a template that I took that creates a TCPtable. I was trying to add a disconnecting function. However, it does not disconnect.

    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我还不会笑 2025-01-11 23:41:28

我无法解决它,但使用 CurrPorts 找到了替代解决方案

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

I could not solve it but found an alternative solution using CurrPorts

 Shell(Application.StartupPath & "\cports /close * * " & GetIpFromLong(TcpTableRow.dwRemoteAddr) & " " & GetTcpPortNumber(TcpTableRow.dwRemotePort))
不乱于心 2025-01-11 23:41:28

我不确定这是否是相同的情况,但我正在使用:
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

这段代码解决了我的问题,但我不确定这是否就是您所说的。

I am not sure if this is the same situation or not but I was using:
session = New Socket(,,,) as my means of connecting via TCP PORT23 and my issue was that I too could not get the connection to close for some reason. I did try the CurrPorts workaround above but I found that did not meet my expectations. Instead I am using 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

This code solved my issue but I am not sure if thats what you where even talking about.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文