Thanks. I have since figured out that I will need to find the first available port and pass that to the ForwardedPortLocal constructor.
I was looking at the code for the local port forward and see that a TCPListener is being used with the address and port that is passed in. To my knowledge, if the TCPListener is given port 0, it will find the first available. This is the method I use to find the first available port. The trouble is that trying to find the first availabale port outside of the port forward class results in a race condition (the port could be taken in the time between when it is discovered as free and when the port forward class creates the listener).
I was thinking it would be nice to have the BoundPort property on the ForwardedPortLocal class be used to get the port from the IPEndPoint that is created when Start() is called on the ForwardedPortClass. I can then use the resulting port when creating a socket connection, which is what I am doing.
The code for getting the port from the listener is simply: BoundPort = ((IPEndPoint) listener.LocalEndpoint).Port
In cases where the port passed in is not zero, then the endpoint port should be the same as what was passed in. If the port passed in is zero, then BoundPort will return the actual port that is being used.
To go a step further, a new constructor could be created that doesn't take a port parameter at all. In this case zero would be the default.
Anybody have any thoughts about this.
I was looking at the code for the local port forward and see that a TCPListener is being used with the address and port that is passed in. To my knowledge, if the TCPListener is given port 0, it will find the first available. This is the method I use to find the first available port. The trouble is that trying to find the first availabale port outside of the port forward class results in a race condition (the port could be taken in the time between when it is discovered as free and when the port forward class creates the listener).
I was thinking it would be nice to have the BoundPort property on the ForwardedPortLocal class be used to get the port from the IPEndPoint that is created when Start() is called on the ForwardedPortClass. I can then use the resulting port when creating a socket connection, which is what I am doing.
The code for getting the port from the listener is simply: BoundPort = ((IPEndPoint) listener.LocalEndpoint).Port
In cases where the port passed in is not zero, then the endpoint port should be the same as what was passed in. If the port passed in is zero, then BoundPort will return the actual port that is being used.
To go a step further, a new constructor could be created that doesn't take a port parameter at all. In this case zero would be the default.
Anybody have any thoughts about this.







