[tests] Avoid "Address already in use"
[mono.git] / mcs / class / test-helpers / NetworkHelpers.cs
1 using System;
2 using System.Net;
3 using System.Net.Sockets;
4
5 namespace MonoTests.Helpers {
6
7         public static class NetworkHelpers
8         {
9                 public static int FindFreePort ()
10                 {
11                         TcpListener l = new TcpListener(IPAddress.Loopback, 0);
12                         l.Start();
13                         int port = ((IPEndPoint)l.LocalEndpoint).Port;
14                         l.Stop();
15                         return port;
16                 }
17                 public static IPEndPoint LocalEphemeralEndPoint ()
18                 {
19                         return new IPEndPoint (IPAddress.Loopback, FindFreePort());
20                 }
21         }
22 }