Closed
Description
I understand that in some TCP stacks, if you listen on port "0", a randomly-chosen and available port will be provided for you.
I would like to be able to use port=0
in ForwardedPortDynamic, to guarantee that an available port is chosen, and be able to use the actually bound port in later code. ForwardedPortDynamic
does not update BoundHost
or BoundPort
after the socket starts listening.
using var sshClient = new SshClient(sshHost, sshPort, sshUser, sshPass);
await sshClient.ConnectAsync(token);
// I would like to do this
using var portForward = new ForwardedPortDynamic(0);
// my current solution to choose a random (and hopefully available) port
// using var portForward = new ForwardedPortDynamic((uint)System.Random.Shared.Next(1024, 65535));
sshClient.AddForwardedPort(portForward);
portForward.Start();
// Use FluentFTP to connect to a remote server via the SOCKS proxy created by SSH.NET
var ftpClient = new AsyncFtpClientSocks5Proxy(new FtpProxyProfile()
{
ProxyHost = "127.0.0.1",
ProxyPort = (int)portForward.BoundPort, // I need the port here, but it is still "0"
FtpHost = ftpHost,
FtpPort = (int)ftpPort,
FtpCredentials = new NetworkCredential(ftpUser, ftpPass),
});
await ftpClient.Connect()
...
Is there a way to bind a random port that is guaranteed to be available and re-use it later?
Metadata
Metadata
Assignees
Labels
No labels