Ruby で 簡単なタイムアウト付きクライアントプログラムを
参考書を参考に作成しています。
以下がそのコードです。
ruby
1require 'socket' 2require 'timeout' 3 4 5host = if ARGV[0] then ARGV[0] else 'localhost' end 6port = if ARGV[1] then ARGV[1] else 'echo' end 7 8sock = nil 9timeout(200){ 10 sock = TCPSocket.open(host,port) 11} 12 13while msg = STDIN.gets 14 cnt = 0 15 begin 16 sock.write(msg) 17 timeout(2) { 18 print sock.gets 19 } 20rescue TimeoutError 21 if cnt < 3 22 cnt +=1 23 STDERR.print "time outerror! #{cnt}" 24 retry 25 else 26 raise 27 end 28end 29end 30sock.close 31
これを実行してみると
cmd
1server.rb:9:in `<main>': Object#timeout is deprecated, use Timeout.timeout instead. 2server.rb:10:in `initialize': No connection could be made because the target machine actively refused it. - connect(2) for "localhost" port echo (Errno::ECONNREFUSED) 3 from server.rb:10:in `open' 4 from server.rb:10:in `block in <main>' 5 from C:/Ruby24-x64/lib/ruby/2.4.0/timeout.rb:93:in `block in timeout' 6 from C:/Ruby24-x64/lib/ruby/2.4.0/timeout.rb:33:in `block in catch' 7 from C:/Ruby24-x64/lib/ruby/2.4.0/timeout.rb:33:in `catch' 8 from C:/Ruby24-x64/lib/ruby/2.4.0/timeout.rb:33:in `catch' 9 from C:/Ruby24-x64/lib/ruby/2.4.0/timeout.rb:108:in `timeout' 10 from C:/Ruby24-x64/lib/ruby/2.4.0/timeout.rb:122:in `timeout' 11 from server.rb:9:in `<main>'
エラーが出ました。。。
英語が読めないのでGoogle翻訳より
なんか廃止とか書かれているのですが実際にもう使えないのでしょうか?
調べても 廃止 について言及してそうなのはリファレンス以外ないですし。。。
実際にもう使えないのか 代わりのものはないのか?
について わからないので
参考になるリンクまたは説明をお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/16 12:40