vb.net 第四节 获取当前TCP连接情况 GetTcpTable

Posted VB.Net

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vb.net 第四节 获取当前TCP连接情况 GetTcpTable相关的知识,希望对你有一定的参考价值。

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

第四节 获取当前TCP连接情况 GetTcpTable

Imports System.Runtime.InteropServices

Public Class Form5
    'typedef struct _MIB_TCPTABLE 
    '  DWORD dwNumEntries; 
    '  MIB_TCPROW table[ANY_SIZE]; 
    ' MIB_TCPTABLE, *PMIB_TCPTABLE;

    'typedef struct _MIB_TCPROW 
    '  DWORD dwState; 
    '  DWORD dwLocalAddr; 
    '  DWORD dwLocalPort; 
    '  DWORD dwRemoteAddr; 
    '  DWORD dwRemotePort; 
    ' MIB_TCPROW, *PMIB_TCPROW;



    'Type MIB_TCPTABLE
    '    dwNum_Of_Entries As Long			'当前包含MIB_TCPROW类型的总数
    '    TCP_Table() As MIB_TCPROW 	'指向包含MIB_TCPROW类型的指针
    'End Type

    'Type MIB_TCPROW
    '    dwState As Long 			' TCP连接状态,为以下值之一:
    '        常量名称	值	说明
    '        MIB_TCP_STATE_CLOSED	1	关闭
    '        MIB_TCP_STATE_LISTEN	2	正在监听
    '        MIB_TCP_STATE_SYN_SENT	3	同步发送
    '        MIB_TCP_STATE_SYN_RCVD	4	同步接收
    '        MIB_TCP_STATE_ESTAB	5	已建立
    '        MIB_TCP_STATE_FIN_WAIT1	6	FINWAIT 1
    '        MIB_TCP_STATE_FIN_WAIT2	7	FINWAIT 2
    '        MIB_TCP_STATE_CLOSE_WAIT	8	关闭等待
    '        MIB_TCP_STATE_CLOSING	9	正在关闭
    '        MIB_TCP_STATE_LAST_ACK	10	最后一次确认
    '        MIB_TCP_STATE_TIME_WAIT	11	时间等待
    '        MIB_TCP_STATE_DELETE_TCB	12	删除连接
    '    dwLocalAddr As Long 		'本地IP
    '    dwLocalPort As Long 		'本地端口
    '    dwRemoteAddr As Long 	'远程机器IP
    '    dwRemotePort As Long 	'远程机器端口
    ' End Type

    'DWORD GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize,   BOOL bOrder);
    'Private Declare Function GetTcpTable Lib "iphlpapi.dll" (ByRef pTcpTable As MIB_TCPTABLE, ByRef pdwSize As Long, ByVal bOrder As Long) As Long


    Structure MIB_TCPTABLE
        Dim dwNum_Of_Entries As Integer            '当前包含MIB_TCPROW类型的总数
        Dim TCP_Table() As MIB_TCPROW   '指向包含MIB_TCPROW类型的指针
    End Structure

    <StructLayout(LayoutKind.Sequential)>
    Structure MIB_TCPROW
        Dim dwState As Integer             ' TCP连接状态
        Dim dwLocalAddr As Integer         '本地IP
        Dim dwLocalPort As Integer         '本地端口
        Dim dwRemoteAddr As Integer    '远程机器IP
        Dim dwRemotePort As Integer    '远程机器端口
    End Structure

    Private Declare Function GetTcpTable Lib "iphlpapi.dll" (ByRef pTcpTable As IntPtr, ByRef pdwSize As Integer, ByVal bOrder As Integer) As Integer
    Private Declare Function GetTcpTable Lib "iphlpapi.dll" (ByVal TcpTable() As Byte, ByRef pdwSize As Integer, ByVal bOrder As Integer) As Integer

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim pMIB_TCPTABLE As IntPtr = IntPtr.Zero
        Dim dwSize As Integer

        Dim returnValue As Integer
        '第一次调用,返回122:ERROR_INSUFFICIENT_BUFFER,缓冲区大小不足
        'pdwSize参数返回实际需要的字节大小
        returnValue = GetTcpTable(pMIB_TCPTABLE, dwSize, True)

        Dim bufferMIB_TCPTABLE(dwSize - 1) As Byte

        '第二次调用获得数据
        returnValue = GetTcpTable(bufferMIB_TCPTABLE, dwSize, True)

        '获得数量
        Dim bytesNumEntries(3) As Byte
        Array.Copy(bufferMIB_TCPTABLE, 0, bytesNumEntries, 0, 4)
        Dim size As Integer = Marshal.SizeOf(bytesNumEntries(0)) * bytesNumEntries.Length
        Dim ptrNumEntries As IntPtr = Marshal.AllocHGlobal(size)
        Marshal.Copy(bytesNumEntries, 0, ptrNumEntries, 4)
        Dim NumEntries As Integer
        NumEntries = Marshal.ReadInt32(ptrNumEntries)

        Dim MIBTr As MIB_TCPROW
        Dim sizeMIBTr As Integer = Marshal.SizeOf(MIBTr)

        For i As Integer = 0 To NumEntries - 1
            Dim pMIB_TCPROW As New IntPtr
            pMIB_TCPROW = Marshal.AllocHGlobal(sizeMIBTr)
            Marshal.Copy(bufferMIB_TCPTABLE, 4 + sizeMIBTr * i, pMIB_TCPROW, sizeMIBTr)
            MIBTr = Marshal.PtrToStructure(pMIB_TCPROW, GetType(MIB_TCPROW))

            TextBox1.Text &= IntToIP(MIBTr.dwLocalAddr) & ":" & MIBTr.dwLocalPort & "->" &
                             IntToIP(MIBTr.dwLocalAddr) & ":" & MIBTr.dwLocalPort & " " &
                             MIBTr.dwState & vbCrLf
        Next

    End Sub

    'Integer型IP转字符串
    Private Function IntToIP(ByVal intAddr As Integer) As String
        Dim bteIp() As Byte
        bteIp = BitConverter.GetBytes(intAddr)
        Dim ip As String = ""
        For i As Integer = 0 To bteIp.Length - 1
            ip &= bteIp(i).ToString & "."
        Next

        Return ip.Substring(0, ip.Length - 1)
    End Function

End Class

 

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

以上是关于vb.net 第四节 获取当前TCP连接情况 GetTcpTable的主要内容,如果未能解决你的问题,请参考以下文章

传输层-第四节:TCP流量控制

传输层-第四节:TCP流量控制

Java网络编程-第四节:TCP流套接字(ServerSocket)编程

Java网络编程-第四节:TCP流套接字(ServerSocket)编程

vb.net 第十四节 返回本机网络接口数量 GetNumberOfInterfaces

如何在 VB.NET 中检查 TCP 服务器(套接字)与 TCP 客户端的连接状态