vb.net 教程 7-1 本地网络信息的获取 4 TCP/UDP连接情况

版权声明:本文为博主原创文章,转载请显著位置标明出处,未经博主允许不得用于商业目的。 https://blog.csdn.net/UruseiBest/article/details/81811628

获得本地网络连接情况也是通过IPGlobalProperties类来完成的,

通过 GetActiveTcpConnections获得所有的TCP连接的信息(TcpConnectionInformation类)
通过 GetActiveTcpListeners 获得TCP侦听信息(IPEndPoint类) 
通过 GetActiveUdpListeners 获得UDP侦听信息(IPEndPoint类) 
然后分别用for each来循环获得对应类的信息

具体代码如下:

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim IPGProperties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties

        TextBox1.Text = "TCP连接:===================" & vbCrLf
        For Each tcpConnectionInfo As TcpConnectionInformation In IPGProperties.GetActiveTcpConnections
            TextBox1.Text &= tcpConnectionInfo.LocalEndPoint.Address.ToString & ":" & tcpConnectionInfo.LocalEndPoint.Port.ToString & "-" _
                                & tcpConnectionInfo.RemoteEndPoint.Address.ToString & tcpConnectionInfo.RemoteEndPoint.Port.ToString _
                                & " " & getState(tcpConnectionInfo.State) & vbCrLf
        Next

        TextBox1.Text &= "TCp侦听:===================" & vbCrLf
        For Each tcpIPEP As IPEndPoint In IPGProperties.GetActiveTcpListeners
            TextBox1.Text &= tcpIPEP.Address.ToString & ":" & tcpIPEP.Port.ToString & vbCrLf
        Next

        TextBox1.Text &= "Udp侦听:===================" & vbCrLf
        For Each udpIPEP As IPEndPoint In IPGProperties.GetActiveUdpListeners
            TextBox1.Text &= udpIPEP.Address.ToString & ":" & udpIPEP.Port.ToString & vbCrLf
        Next
    End Sub

 运行如下:

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

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

猜你喜欢

转载自blog.csdn.net/UruseiBest/article/details/81811628
今日推荐