我正在运行一个服务器/客户端程序,我不断收到一个我不明白的错误:
FishClientRun.rb:18:in `<main>': undefined method `chomp' for nil:NilClass (NoMethodError)
Shizuo:FISHGAME2 macowner$
但是,代码运行一次。它实际上接收卡并将它们存储得很好。当客户来回循环并挂在 get 语句上incoming = client.socket.gets.chomp
。它不喜欢.chomp.
如果我使用.chomp
,它会像预期的那样挂起而不会引发错误,但是随后我得到了换行符并得到了错误的输入client.input_decision(incoming)
。
我的客户:
client = ClientClass.new('localhost', 2012)
while true
puts "IN WHILE TRUE!!!"
incoming = ""
incoming = client.socket.gets.chomp
puts "INPUT RECEIVED:::::::: #{incoming}"
client.input_decision(incoming)
end
这是服务器。我认为这部分还可以:
server = SocketServerClass.new(2012, number_of_players)
number_of_players.times do
server.accept_client(server.fish_game)
end
puts "Clients accepted"
server.update_client_cards(server.fish_game)
puts "client cards sent to server"
while true
sleep(10)
break
end