How to get a free port ?

I would like to get a free port for an application. I do not care which port it is. Ingo Rammer in a 2001 post provided a hacky solution to this issue. I would like to know if there is cleaner  (threadsafe etc.) solutions available

Will .Net 2.0 provides a native helper to solve this issue

Thanks in advance,
Joannes


Answer this question

How to get a free port ?

  • Michiel0099

    Ok, thanks a lot, I will try this. But I am curious where did you get this info from I mean, this point is not mentionned in the documentation (even the author of the book Advance .Net Remoting did ignore this element)

    Also I have an additional question, how can I know on which port the TcpChannel has been instanciated

    Joannes

  • alexgr

    What I'm missing from this discussion is if you create a server channel and cause it to pick a port for you, how does the client know how to construct the Activator.GetObject(...) URL parameter which usually contains the port the server object is using in its channel transport. E.g. client URL: "tcp://localhost:7777/RemoteObjURI.rem"

    Or do you do the same in client Activator as the server channel instanciation, i.e. use the following URL: "tcp://localhost:0/RemoteObjURI.rem" or "tcp://localhost/RemoteObjURI.rem"

    Thanks in advance.


  • ncj

    I don't believe Remoting can do this for you, you need an out of band mechanism to let the client know what the port is.

  • JulesW

     Joannes Vermorel wrote:
    Ok, thanks a lot, I will try this. But I am curious where did you get this info from I mean, this point is not mentionned in the documentation
    Following the documentation link you provided, the first sentence in the Remarks section says "To request that the remoting system choose an open port on your behalf, specify port "0" (zero). "

    Also I have an additional question, how can I know on which port the TcpChannel has been instanciated
    For TcpChannel I think you'd have to use ChannelData to retrieve the channel URI and parse that.

    If you aren't using remoting then a TcpListener might be a better class to use.
    http://msdn2.microsoft.com/library/c4zw1w3a(en-us,vs.80).aspx  With TcpListener, you can still use zero to let the system pick your port number and then use the LocalEndpoint property to find out what port number you got.

  • mydarlingdarla

    Simply pass zero to the constructor of the TcpChannel. The system will then worry about finding a free port. This works in .NET 1.0/1.1 as well as .NET 2.0.

  • tombraider

    what you describe is a discovery mechanism. remoting doesn't and will likely never have support for discovery.

    WCF doesn't have support for discovery either in the current release, though there is a sample that allows you to use it:

    http://wcf.netfx3.com/files/folders/protocol_channels/entry7909.aspx



  • Louis315

    Thank you ranamauro for your response. I do believe you are correct. However, just for grins I'm going to test my theory.

    So the CLR remoting channel mechanism can't simply lookup an endpoint by the URI, the "foo.rem" part of the URL, amongst all the registered endpoints on the server designated by the URL It seems to me that unique registered URIs could be enforced for those server channel requests that pick a port on the caller's behalf by having the registration process return an error for a duplicate endpoint of this kind. Is there anybody out there that is definitive about the answer to my question


  • How to get a free port ?