我正在使用REST API服务运行一些集成测试。
问题在于,有时在下一次测试开始时,硬编码端口有时不可用。因为它是由先前的测试打开的,但尚未被系统关闭。

我使用OWIN,在下一次测试开始时,该应用程序已关闭。

您能否建议我一个好方法来确定系统上的空闲端口,而无需事先打开并关闭它?或者说这是不可能的。

因为它可能尚未被系统释放,就像它已经发生的那样。

最佳答案

作为TempoClick的answer的替代方法,我们可以使用 IPGlobalProperties.GetActiveTcpListeners() 方法测试端口是否可用-无需尝试提前打开它。 GetActiveTcpListeners()返回系统上所有事件的TCP监听器,因此我们可以使用它来确定端口是否空闲。

public bool IsFree(int port)
{
    IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
    IPEndPoint[] listeners = properties.GetActiveTcpListeners();
    int[] openPorts = listeners.Select(item => item.Port).ToArray<int>();
    return openPorts.All(openPort => openPort != port);
}

请注意,GetActiveTcpListeners()不会返回监听的UDP端点,但是我们可以使用GetActiveUdpListeners()来获取它们。

因此,您可以从默认端口开始(或选择一个随机值),并保持递增,直到使用IsFree方法找到可用端口为止。
int NextFreePort(int port = 0)
{
    port = (port > 0) ? port : new Random().Next(1, 65535);
    while (!IsFree(port))
    {
        port += 1;
    }
    return port;
}

一个简单的测试:
using System;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Linq;

class Test
{
    static void Main(string[] args)
    {
        int port = 1000;
        Console.WriteLine(IsFree(port));
        TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), port);
        server.Start();
        Console.WriteLine(IsFree(port));
        Console.WriteLine(NextFreePort(port));
    }

    static bool IsFree(int port)
    {
        IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
        IPEndPoint[] listeners = properties.GetActiveTcpListeners();
        int[] openPorts = listeners.Select(item => item.Port).ToArray<int>();
        return openPorts.All(openPort => openPort != port);
    }

    static int NextFreePort(int port = 0) {
        port = (port > 0) ? port : new Random().Next(1, 65535);
        while (!IsFree(port)) {
            port += 1;
        }
        return port;
    }
}



另一种方法是使用端口零。在这种情况下,系统将从动态端口范围中选择一个随机的空闲端口。我们可以从LocalEndpoint属性获取此端口号。
TcpListener server = new TcpListener(IPAddress.Loopback, 0);
server.Start();
int port = ((IPEndPoint)server.LocalEndpoint).Port;
Console.WriteLine(port);

10-07 19:28
查看更多