I’m using Ruby 1.8.4 on Linux and I’m having trouble with non-blocking
sockets. Socket#accept_nonblock doesn’t exist and using fcntl to set
O_NONBLOCK before calling accept doesn’t seem to work. I’ve tried with
TCPServer and Socket and accept blocks in both cases. What’s the 1.8.4
way to get non-blocking sockets?
Some sample code:
p = 8080
socket = Socket.new Socket::AF_INET, Socket::SOCK_STREAM, 0
sock_addr = Socket.pack_sockaddr_in p, ‘0.0.0.0’
socket.bind sock_addr
socket.listen SERVER_QUEUE_LENGTH
original_flags = socket.fcntl Fcntl::F_GETFL, 0
socket.fcntl(Fcntl::F_SETFL, original_flags | Fcntl::O_NONBLOCK)
connection = socket.accept
I first tried nearly the same thing, but used a TCPServer instead of a
Socket and called fcntl as above before calling accept. In both
examples, the accept call blocks.
I’m new to socket programming so I’m probably just missing something.
Any ideas? Maybe I’ll just upgrade to 1.8.6.
Thanks,
Jeremy
On 8/27/07, [email protected] [email protected] wrote:
I’m using Ruby 1.8.4 on Linux and I’m having trouble with non-blocking
sockets. Socket#accept_nonblock doesn’t exist and using fcntl to set
O_NONBLOCK before calling accept doesn’t seem to work. I’ve tried with
TCPServer and Socket and accept blocks in both cases. What’s the 1.8.4
way to get non-blocking sockets?
Some sample code:
Update to a newer Ruby and use accept_nonblock. As an alternative, look
at
Ruby/EventMachine.
On Aug 27, 8:58 pm, “[email protected]” [email protected] wrote:
sock_addr = Socket.pack_sockaddr_in p, ‘0.0.0.0’
I’m new to socket programming so I’m probably just missing something.
Any ideas? Maybe I’ll just upgrade to 1.8.6.
Thanks,
Jeremy
OK, please disregard my previous message. I have something else wrong
as a minimal example using TCPServer and fcntl does result in a non-
blocking accept.
Now I think I’m hitting an issue with the fact that Ruby doesn’t do
native threads.
Thanks,
Jeremy