プログラム(1.rb)は次のとおりです。
require 'rack'
my_rack = lambda { |env| [200, {}, ["Hello. The time is: #{Time.now}"]] }
handler = Rack::Handler::WEBrick
handler.run(my_rack, :PORT => 12_500)
エラーは次のとおりです。
~/ruby_programs$ ruby 1.rb
[2012-12-07 21:49:09] INFO WEBrick 1.3.1
[2012-12-07 21:49:09] INFO ruby 1.9.3 (2012-04-20) [x86_64-darwin10.8.0]
[2012-12-07 21:49:09] WARN TCPServer Error: Permission denied - bind(2)
[2012-12-07 21:49:09] WARN TCPServer Error: Permission denied - bind(2)
/Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:85:in `initialize': Permission denied - bind(2) (Errno::EACCES)
from /Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:85:in `new'
from /Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:85:in `block in create_listeners'
from /Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:82:in `each'
from /Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:82:in `create_listeners'
from /Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:82:in `listen'
from /Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:70:in `initialize'
from /Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:45:in `initialize'
from /Users/7stud/.rvm/gems/ruby-1.9.3-p194@programming/gems/rack-1.4.1/lib/rack/handler/webrick.rb:10:in `new'
from /Users/7stud/.rvm/gems/ruby-1.9.3-p194@programming/gems/rack-1.4.1/lib/rack/handler/webrick.rb:10:in `run'
from 1.rb:5:in `<main>'
~/ruby_programs$
../webrick/utils.rbの85行目は次のとおりです。
sock = TCPServer.new(ai[3], port)
1.rbのコードを次のように置き換えた場合:
require 'socket'
server = TCPServer.new 12_000 # Server bind to port 2000
loop do
client = server.accept # Wait for a client to connect
client.puts "Hello !"
client.puts "Time is #{Time.now}"
client.close
end
エラーは発生しません。アドレスを入力すると、次のようになります。
http://localhost:12000/
私のブラウザでは、期待どおりの出力が得られます。
Hello !
Time is 2012-12-07 18:58:15 -1000
.rvmディレクトリの権限:
~$ ls -al
total 208
...
drwxr-xr-x 30 7stud staff 1020 Aug 31 11:09 .rvm
...