File: LocalPortsFixture.cs

package info (click to toggle)
golang-github-microsoft-dev-tunnels 0.0.25-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,988 kB
  • sloc: cs: 9,969; java: 2,767; javascript: 328; xml: 186; makefile: 5
file content (32 lines) | stat: -rw-r--r-- 1,088 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System.Net;
using System.Net.Sockets;

namespace Microsoft.DevTunnels.Test
{
    public class LocalPortsFixture : IDisposable
    {
        private readonly TcpListener listener;
        private readonly TcpListener listener1;

        public LocalPortsFixture()
        {
            // Get the local tcp ports
            this.listener = new TcpListener(IPAddress.Loopback, port: 0);
            this.listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, false);
            this.listener.Start();

            this.listener1 = new TcpListener(IPAddress.Loopback, port: 0);
            this.listener1.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, false);
            this.listener1.Start();
        }

        public ushort Port => (ushort)(((IPEndPoint)this.listener.LocalEndpoint).Port);
        public ushort Port1 => (ushort)(((IPEndPoint)this.listener1.LocalEndpoint).Port);

        public void Dispose()
        {
            this.listener.Stop();
            this.listener1.Stop();
        }
    }
}