From: GOTOU Yuuzou Date: 2005-03-21T04:36:12+09:00 Subject: Re: can WEBrick bind to port 0, and then tell me what port was allocated? Hi, In message <20050320184914.GB911@ensemble.local>, `Sam Roberts ' wrote: > INADDR_ANY with a port of zero. The system will select a free port. > > Then, I can advertise that port using mDNS/DNS-SD. > > Is this possible? I can maybe set :Port to 0, but I am searching the > webrick src and can't see a way to get the underlying socket up, so I > can ask it it's port. Setting :Port to 0 works like you want. However WEBrick never report it. I think the log messages should be changed. How about the following patch? --- lib/webrick/server.rb 7 Mar 2005 12:32:07 -0000 1.9 +++ lib/webrick/server.rb 20 Mar 2005 19:17:49 -0000 @@ -74,11 +78,16 @@ def listen(address, port) def start(&block) raise ServerError, "already started." if @status != :Stop server_type = @config[:ServerType] || SimpleServer server_type.start{ - @logger.info \ - "#{self.class}#start: pid=#{$$} port=#{@config[:Port]}" + @logger.info "#{self.class}#start: pid=#{$$}" + @listeners.each{|sock| + sockaddr = sock.addr + addr = sockaddr[3] + port = sockaddr[1] + @logger.info "#{self.class}#start: addr=#{addr} port=#{port}" + } call_callback(:StartCallback) thgroup = ThreadGroup.new -- gotoyuzo