in reply to Re: What's the best way to find an unused TCP port on the local system
in thread What's the best way to find an unused TCP port on the local system

Hmm... if I just want to know which next port (starting from e.g. 1025) is free, but without binding to it (e.g. to pass the port number to an external program) - how can that be done? I'm doing an (unportable) system "lsof -i :$port" in ssh chain to check whether it's busy. Is there a better (portable) way?

cheers,

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
  • Comment on Re^2: What's the best way to find an unused TCP port on the local system
  • Download Code

Replies are listed 'Best First'.
Re^3: What's the best way to find an unused TCP port on the local system
by ikegami (Patriarch) on Oct 22, 2006 at 21:13 UTC
    No, it's not possible. Such a feature would unavoidably introduce a race condition. Your child process needs to either communicate the port back to you, or must inherit the opened socket from you.