non-blocking socket in Ruby 1.8.4?

Y

yermej

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
 
Y

yermej

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top