Skip to content

Commit

Permalink
Check for avail port
Browse files Browse the repository at this point in the history
  • Loading branch information
thohng committed Mar 21, 2024
1 parent 54eb4b9 commit 15fe559
Showing 1 changed file with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
Expand Down Expand Up @@ -43,29 +44,70 @@ private static void AuthenticateCertificate(X509Certificate2 certificate)
}
}

private static void ClientServerAuthenticate(X509Certificate2 certificate)



private static async void ClientServerAuthenticate(X509Certificate2 certificate)
{
const string plainText = "Hello, world! こんにちは世界 ഹലോ വേൾഡ് Kαληµε´ρα κο´σµε";
var plainMessage = Encoding.UTF8.GetBytes(plainText);
var port1 = 19000;
var port2 = 19999;

int FindPort(int port1, int port2)
{
var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
var tcpListeners = ipGlobalProperties.GetActiveTcpListeners();
var flags = new bool[port2 - port1 + 1];
Array.Fill(flags, true);

foreach (var tcpListener in tcpListeners)
{
if (IPAddress.IsLoopback(tcpListener.Address))
{
for (var port = port1; port <= port2; port++)
{
if (tcpListener.Port == port)
{
flags[port - port1] = false;
}
}
}
}

for (var port = port1; port <= port2; port++)
{
if (flags[port - port1])
{
return port;
}
}

return -1;
}

var port = FindPort(port1, port2);

// https://stackoverflow.com/questions/28548326/net-sslstream-with-client-certificate
// https://code-maze.com/csharp-task-run-vs-task-factory-startnew/

Assert.InRange(port, port1, port2);

var clientCompleted = new TaskCompletionSource<int>();
var serverInitialized = new TaskCompletionSource<int>();
Exception? clientFault = null;
Exception? serverFault = null;
string? clientReceived = null;
string? serverReceived = null;

async Task DoServer(CancellationToken token, Task taskWaitClientCompleted)
async Task DoServer(int port, CancellationToken token, Task taskWaitClientCompleted)
{
var server = new TcpListener(IPAddress.Loopback, 9843);
var server = new TcpListener(IPAddress.Loopback, port);
try
{
server.Start();
serverInitialized.SetResult(0);
using TcpClient client = server.AcceptTcpClient();
using var client = server.AcceptTcpClient();

#if NETCOREAPP3_1
using var ssltrream = new SslStream(client.GetStream(), false, (s, cm, ch, p) => true);
Expand Down Expand Up @@ -108,15 +150,15 @@ async Task DoServer(CancellationToken token, Task taskWaitClientCompleted)
}
}

async Task DoClient(CancellationToken token, Task taskWaitServerInitialized)
async Task DoClient(int port, CancellationToken token, Task taskWaitServerInitialized)
{
try
{
await taskWaitServerInitialized;
await Task.Delay(TimeSpan.FromMilliseconds(10), token);
{
using TcpClient client = new();
await client.ConnectAsync(IPAddress.Loopback, 9843);
await client.ConnectAsync(IPAddress.Loopback, port);

#if NETCOREAPP3_1
using var ssltrream = new SslStream(client.GetStream(), false, (s, cm, ch, p) => true);
Expand Down Expand Up @@ -150,8 +192,8 @@ async Task DoClient(CancellationToken token, Task taskWaitServerInitialized)

var cts = new CancellationTokenSource();

var clientWork = Task.Run(() => DoClient(cts.Token, serverInitialized.Task));
var serverWork = Task.Run(() => DoServer(cts.Token, clientCompleted.Task));
var clientWork = Task.Run(() => DoClient(port, cts.Token, serverInitialized.Task));
var serverWork = Task.Run(() => DoServer(port, cts.Token, clientCompleted.Task));

Task.WaitAny(new[] { Task.WhenAll(clientWork, serverWork) }, TimeSpan.FromSeconds(20));
cts.Cancel();
Expand Down

0 comments on commit 15fe559

Please sign in to comment.