From: Sam Roberts Date: 2005-03-21T23:54:59+09:00 Subject: Re: can WEBrick bind to port 0, and then tell me what port was allocated? Quoting gotoyuzo@notwork.org, on Mon, Mar 21, 2005 at 05:11:24PM +0900: > In message <20050320203133.GA1012@ensemble.local>, > `Sam Roberts ' wrote: > > Is there really a case when you would want every listener to have a > > different port # assigned by the network stack? If not, maybe setting > > :Port to 0 should have the above behaviour. > > I prefer nil than 0 for this behavior. It the specified port I can understand not liking 0, I think a :Port of zero would best be considered a bug, and raise an error. > is nil, listening port will be chosen and tried to bind until > succeed like this: I'm not sure about nil. It is very sensitive to error, it is very common to pass nil when you failed to initialize something correctly, and now it will silently do something unexpected, and the caller will waste time trying to figure out why every port is different, and having to ask on ruby-talk, where you will tell him he probably passed nil... and everybody will waste time. Personally, I would prefer :auto. You don't accidently pass a symbol in as a port number, doing so is a pretty clear indication to WEBrick, and to readers of the code that the port is special. > module WEBrick > class GenericServer > def listen(addr, port) > return auto_listen(addr) unless port > @listeners += Utils.create_listeners(addr, port, @logger) > end I don't understand the following code at all. Why are you choosing the port number, when the network stack will do it for you much more portably and efficiently? And don't some net stacks not allow port past the 16K range? > def auto_listen(addr) > port = 65535 > while port > 1023 > begin > listen(addr, port) > rescue Errno::EACCES, Errno::EADDRINUSE > port -= 1 > retry > end > @config[:Port] = port > return > end > end > end > end > > If nobody opposes, I can make it as a default behavior of > WEBrick. > > -- > gotoyuzo >