这是程序(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
...