# Block reading from socket and STDIN

**URL:** <https://www.ruby-forum.com/t/block-reading-from-socket-and-stdin/197217>\
**Category:** Ruby\
**Created:** [February 26, 2009, 10:33am UTC](https://www.ruby-forum.com/t/block-reading-from-socket-and-stdin/197217 "2009-02-26T10:33:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![massimiliano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@massimiliano](https://www.ruby-forum.com/u/massimiliano)\
**Post date:** [February 26, 2009, 10:33am UTC](https://www.ruby-forum.com/t/block-reading-from-socket-and-stdin/197217/1 "2009-02-26T10:33:20Z")

</div>

Hi I am pretty new in ruby, may be this topic has already been discussed  
here. But I start running out of resources.  
I am on a Windows XP PC and I am using ruby186-26.

I am writing a client/Multiplexing Server socket application,  
(I took most of the code from the manual).

The server application, beside to listening on the socket via the  
select(…), must listening at the standard input. This is because a  
user has to give input to the server while he is handling the socket  
connection to clients.

The problem I found is that the select(…) and the STDIN.gets methos  
are both blocking also if I run it on two different threads.

Many thanks

---

<div class="post-metadata">

**Author:** ![massimiliano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@massimiliano](https://www.ruby-forum.com/u/massimiliano)\
**Post date:** [February 26, 2009, 11:02am UTC](https://www.ruby-forum.com/t/block-reading-from-socket-and-stdin/197217/2 "2009-02-26T11:02:36Z")

</div>

On 26 Feb 2009, at 09:32, Giuseppe Parlavecchio wrote:

> user has to give input to the server while he is handling the socket  
> connection to clients.
> 
> The problem I found is that the select(…) and the STDIN.gets methos  
> are both blocking also if I run it on two different threads.

This is off the top of my head, so apologies if it’s wrong, but as  
STDIN is a regular IO object you _should_ be able to handle its input  
from the same select(…) call as the server socket.

Ellie

## Eleanor McHugh Games With Brains [http://slides.games-with-brains.net](http://slides.games-with-brains.net)

raise ArgumentError unless @reality.responds\_to? :reason

---

<div class="post-metadata">

**Author:** ![massimiliano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@massimiliano](https://www.ruby-forum.com/u/massimiliano)\
**Post date:** [February 26, 2009, 12:41pm UTC](https://www.ruby-forum.com/t/block-reading-from-socket-and-stdin/197217/3 "2009-02-26T12:41:26Z")

</div>

Pretty sure gets doesn’t actually block. I believe it is implemented  
with a  
select internally.

Try this as a test  
Thread.new {  
loop {  
puts "loop "  
}  
}  
STDIN.gets

If it stops then gets is blocking

On Thu, Feb 26, 2009 at 8:01 PM, Eleanor McHugh \<

---

<div class="post-metadata">

**Author:** ![massimiliano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@massimiliano](https://www.ruby-forum.com/u/massimiliano)\
**Post date:** [February 28, 2009, 9:42pm UTC](https://www.ruby-forum.com/t/block-reading-from-socket-and-stdin/197217/4 "2009-02-28T21:42:10Z")

</div>

Giuseppe Parlavecchio wrote:

> connection to clients.
> 
> The problem I found is that the select(…) and the STDIN.gets methos  
> are both blocking also if I run it on two different threads.
> 
> Many thanks

Windows + STDIN.gets == ruby blocks other threads, sorry to say.

At least, that’s my experience in ruby186. Maybe this has changed more  
recently?
