Error while trying to use Socket#accept_nonblock

Pretty much sample code included with ri docs:

#Server

In one script, start this first

require ‘socket’
include Socket::Constants
socket = Socket.new(AF_INET, SOCK_STREAM, 0)
sockaddr = Socket.sockaddr_in(2200, ‘localhost’)
socket.bind(sockaddr)
socket.listen(5)
begin
client_socket, client_sockaddr = socket.accept_nonblock
rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR
IO.select([socket])
retry
end
puts “The client said, ‘#{socket.readline.chomp}’” #line number 14
client_socket.puts “Hello from script one!”
socket.close

#client

In another script, start this second

require ‘socket’
include Socket::Constants
socket = Socket.new(AF_INET, SOCK_STREAM, 0)
sockaddr = Socket.sockaddr_in(2200, ‘localhost’)
socket.connect(sockaddr)
socket.puts “Hello from script 2.”
sleep(2)
puts “The server said, ‘#{socket.readline.chomp}’”
socket.close

When i run the above code, the server code exits with an exception :

non_block_server.rb:14:in `readline’: Transport endpoint is not
connected (Errno::ENOTCONN)
from non_block_server.rb:14

Any ideas, why?

Hemant K. wrote:

Pretty much sample code included with ri docs:

Typo?

rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR
IO.select([socket])
retry
end
puts “The client said, ‘#{socket.readline.chomp}’” #line number 14

                          ^^^^^^

sub(“socket”, “client_socket”)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs