You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Offer/expose dynamic port assignment for ForwardedPortDynamic #1575
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.
usingvarsshClient=newSshClient(sshHost,sshPort,sshUser,sshPass);awaitsshClient.ConnectAsync(token);// I would like to do thisusingvarportForward=newForwardedPortDynamic(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.NETvarftpClient=newAsyncFtpClientSocks5Proxy(newFtpProxyProfile(){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=newNetworkCredential(ftpUser,ftpPass),});awaitftpClient.Connect()...
Is there a way to bind a random port that is guaranteed to be available and re-use it later?
Activity
Rob-Hague commentedon Jan 18, 2025
I think it is probably just an oversight. The
BoundPortis updated inForwardedPortLocal:SSH.NET/src/Renci.SshNet/ForwardedPortLocal.cs
Lines 199 to 200 in 7e71bb4
but not in
ForwardedPortDynamic