Question

I'm running a server/client program, I keep getting an error I don't understand:

FishClientRun.rb:18:in `<main>': undefined method `chomp' for nil:NilClass (NoMethodError)
Shizuo:FISHGAME2 macowner$ 

However, the code runs once. It actually receives the cards and stores them just fine. When the client comes loops back around and hangs on the get statement incoming = client.socket.gets.chomp. It doesn't like the .chomp. If I take of the .chomp, it hangs like it is supposed to without throwing an error, but then I get the newline character and get bad input for client.input_decision(incoming).

My client:

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

Here's the server. I think this part is okay:

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
Was it helpful?

Solution

gets will return nil if it encounters an end of file. Presumably, you want to terminate at that point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top