A
andreas.baus
Hi. I am trying to set up a TcpListener to listen for incoming TCP
connections, and one thing I have been wondering is whether there's a
good way for finding an available port number, say, from a given range
of ports.
Of course I could just loop over all ports, try to open the listener on
it, and catch the SocketException that gets thrown when the port is
already in use, before attempting the next port number and so on until
the Listener creation succeeds (or the entire range is exhausted). But
that hardly strikes me as efficient or elegant.
If anyone has any ideas for a better way, I'd love to hear them. Thanks
in advance.
connections, and one thing I have been wondering is whether there's a
good way for finding an available port number, say, from a given range
of ports.
Of course I could just loop over all ports, try to open the listener on
it, and catch the SocketException that gets thrown when the port is
already in use, before attempting the next port number and so on until
the Listener creation succeeds (or the entire range is exhausted). But
that hardly strikes me as efficient or elegant.
If anyone has any ideas for a better way, I'd love to hear them. Thanks
in advance.