# Non-blocking socket in Ruby 1.8.4?

**URL:** <https://www.ruby-forum.com/t/non-blocking-socket-in-ruby-1-8-4/113148>\
**Category:** Ruby\
**Created:** [August 28, 2007, 4:00am UTC](https://www.ruby-forum.com/t/non-blocking-socket-in-ruby-1-8-4/113148 "2007-08-28T04:00:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 28, 2007, 4:00am UTC](https://www.ruby-forum.com/t/non-blocking-socket-in-ruby-1-8-4/113148/1 "2007-08-28T04:00:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 28, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/non-blocking-socket-in-ruby-1-8-4/113148/2 "2007-08-28T14:13:08Z")

</div>

On 8/27/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 28, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/non-blocking-socket-in-ruby-1-8-4/113148/3 "2007-08-28T17:41:40Z")

</div>

On Aug 27, 8:58 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
