使用指定的端口号获取当前的IP地址

时间:2019-07-13 09:49:23

标签: vb.net tcp ip port

我在vb.net中做了一个简单的TCP客户端-服务器设置。我想知道是否可以使用指定的端口号返回当前的所有IP地址,我将如何处理?

1 个答案:

答案 0 :(得分:1)

这是从IPGlobal​Properties.​Get​Active​Tcp​Connections Method转换而来的VB.NET解决方案。

Imports System.Net.NetworkInformation

Module Module1

    Sub Main()
        ShowActiveTcpConnections()
    End Sub

    Public Sub ShowActiveTcpConnections()
        Debug.WriteLine("Active TCP Connections")
        Dim properties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties
        Dim connections() As TcpConnectionInformation = properties.GetActiveTcpConnections
        For Each c As TcpConnectionInformation In connections
            Debug.WriteLine("{0} <==> {1}", c.LocalEndPoint.ToString, c.RemoteEndPoint.ToString)
        Next
    End Sub

End Module

输出示例:

Active TCP Connections
127.0.0.1:1028 <==> 127.0.0.1:5354
127.0.0.1:1029 <==> 127.0.0.1:5354
127.0.0.1:1055 <==> 127.0.0.1:27015
127.0.0.1:1069 <==> 127.0.0.1:27015
127.0.0.1:1071 <==> 127.0.0.1:27015
127.0.0.1:1080 <==> 127.0.0.1:27015
127.0.0.1:1081 <==> 127.0.0.1:5354
127.0.0.1:1082 <==> 127.0.0.1:5354
127.0.0.1:1084 <==> 127.0.0.1:1085
127.0.0.1:1085 <==> 127.0.0.1:1084
127.0.0.1:1154 <==> 127.0.0.1:27015
127.0.0.1:5354 <==> 127.0.0.1:1028