-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTestUtilities.cs
More file actions
24 lines (21 loc) · 782 Bytes
/
TestUtilities.cs
File metadata and controls
24 lines (21 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System.Net;
using System.Net.Sockets;
namespace DnsClientX.Tests {
internal static class TestUtilities {
public static int GetFreePort() {
return GetFreeTcpPort();
}
public static int GetFreeTcpPort() {
TcpListener listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
int port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
public static int GetFreeUdpPort() {
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
return ((IPEndPoint)socket.LocalEndPoint!).Port;
}
}
}