版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
获得本地网络连接情况也是通过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教程 目录
————————————————
版权声明:本文为CSDN博主「VB.Net」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。