首页 > 解决方案 > 获取当前使用指定端口号的IP地址

问题描述

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

标签: vb.nettcpipport

解决方案


这里是翻译自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

推荐阅读