# Non blocking socket keep blocking on read?

**URL:** <https://rubytalk.org/t/non-blocking-socket-keep-blocking-on-read/49398>\
**Category:** ruby-talk\
**Created:** [1 October 2008 13:33 UTC](https://rubytalk.org/t/non-blocking-socket-keep-blocking-on-read/49398 "2008-10-01T13:33:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serge\_Savoie](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Serge\_Savoie](https://rubytalk.org/u/Serge_Savoie)\
**Post date:** [1 October 2008 13:33 UTC](https://rubytalk.org/t/non-blocking-socket-keep-blocking-on-read/49398/1 "2008-10-01T13:33:47Z")

</div>

Hello everyone,

I have a problem with a socket that block (and my process alt) when the  
server stop send me stuff. The server send me a couple of things ( file  
name and so on ) and after that it send me a file ( a jpeg ). Everything  
works ok but I have to force the read loop to break. How can I detect  
that the server dont send me nothing anymore and break ? I was sure that  
it will do it with connect\_non\_block ?

My problem is in the  
while( buf = socket.readline )  
it is where it hang when the server stop sending...

Here is my complete code...

&nbsp;&nbsp;&nbsp;&nbsp;socket = Socket.new(AF\_INET, SOCK\_STREAM, 0)  
&nbsp;&nbsp;&nbsp;&nbsp;sockaddr = Socket.sockaddr\_in(9401, '10.2.4.92')

&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;socket.connect\_nonblock(sockaddr)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue Errno::EINPROGRESS  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IO.select(nil, [socket])  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;socket.connect\_nonblock(sockaddr)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue Errno::EISCONN  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "sending..."

&nbsp;&nbsp;&nbsp;&nbsp;begin

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;socket.write("207\n")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;socket.write("146316\n")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;socket.write( "89c38b1b197162cae874e5274963633f" + "\n")

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "receiving..."

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i=0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while data = socket.readline  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "data : " + data

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if i == 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Return code : " + data  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if i == 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Bytes to receive : " + data  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nombre\_bytes = data  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if i == 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "File name : " + data  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if i == 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = i+1

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = 0;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while( buf = socket.readline )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "here"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts buf  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts " i = " + i.to\_s;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( @imageData == nil )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@imageData = buf  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@imageData += buf  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = i + 1

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#I Dont want to do this  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#with this all works because I kow the the server ships  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#me the test file in 26 chunk  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#I wanna detect that readline read nothing instead !  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( i == 27 )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;socket.close

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue Exception =\> exc  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "erreur #{exc.message}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts exc.inspect  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;socket.close

&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;end

Thx for your comments !

Seurdge

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Luis1](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@Luis1](https://rubytalk.org/u/Luis1)\
**Post date:** [1 October 2008 13:49 UTC](https://rubytalk.org/t/non-blocking-socket-keep-blocking-on-read/49398/2 "2008-10-01T13:49:18Z")

</div>

Hi Sergey

The server sends you the size of the image, so you can use that to  
check when the download completed. Also check the way you are reading  
the image, you are using IO::readline which is generally used to read  
lines of text. Take a look at IO::read to download the image.

> **···**
>
> --  
> Luis Parravicini  
> [http://ktulu.com.ar/blog/](http://ktulu.com.ar/blog/)

---

<div class="post-metadata">

**Author:** ![Serge\_Savoie](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Serge\_Savoie](https://rubytalk.org/u/Serge_Savoie)\
**Post date:** [1 October 2008 13:53 UTC](https://rubytalk.org/t/non-blocking-socket-keep-blocking-on-read/49398/3 "2008-10-01T13:53:53Z")

</div>

Ths Luis !

But how do I know the weight of the received bytes ? I have think of  
what you propose but can't find the way to "measure" the received stuff  
!!!

I have to use readline or gets to receive because the server (a java  
server) only offer me buffered stream and recv and read dont work in  
this case...

Serge

Luis Parravicini wrote:

> **···**
>
> > Hi Sergey
> > 
> > The server sends you the size of the image, so you can use that to  
> > check when the download completed. Also check the way you are reading  
> > the image, you are using IO::readline which is generally used to read  
> > lines of text. Take a look at IO::read to download the image.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Luis1](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@Luis1](https://rubytalk.org/u/Luis1)\
**Post date:** [1 October 2008 15:08 UTC](https://rubytalk.org/t/non-blocking-socket-keep-blocking-on-read/49398/4 "2008-10-01T15:08:04Z")

</div>

It doesn't matter if the server is using a buffered stream, you'll  
get the bytes on the client anyway. Maybe something like this helps  
(haven't tried it):

data = nil  
left = nombre\_bytes  
while left \> 0  
&nbsp;&nbsp;# 8192 is an arbitrary buffer size  
&nbsp;&nbsp;buf = socket.read( left \> 8192 ? 8192 : left )  
&nbsp;&nbsp;# eof reached  
&nbsp;&nbsp;break unless buf

&nbsp;&nbsp;left -= buf.size  
&nbsp;&nbsp;data += buf  
end

@imageData = data

Why are you using a while to read return code, filename and size?  
Three socket.readline should be enough.

Bye

> **···**
>
> On Wed, Oct 1, 2008 at 10:53 AM, Serge Savoie \<seurdge.sawa@gmail.com\> wrote:
> 
> > But how do I know the weight of the received bytes ? I have think of  
> > what you propose but can't find the way to "measure" the received stuff  
> > !!!
> > 
> > I have to use readline or gets to receive because the server (a java  
> > server) only offer me buffered stream and recv and read dont work in  
> > this case...
> 
> --  
> Luis Parravicini  
> [http://ktulu.com.ar/blog/](http://ktulu.com.ar/blog/)

---

<div class="post-metadata">

**Author:** ![Serge\_Savoie](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Serge\_Savoie](https://rubytalk.org/u/Serge_Savoie)\
**Post date:** [1 October 2008 15:16 UTC](https://rubytalk.org/t/non-blocking-socket-keep-blocking-on-read/49398/5 "2008-10-01T15:16:05Z")

</div>

Thx a lot Luis 😉

The Java server now send me a signal for "end of transmission" so I do  
not have the check the weight of the transfer...

"Why are you using a while to read return code, filename and size? Three  
socket.readline should be enough."

yeah sure, but its just cheesy test code lol

Thx again for your rapid reply and have a nice day !

Luis Parravicini wrote:

> **···**
>
> > On Wed, Oct 1, 2008 at 10:53 AM, Serge Savoie \<seurdge.sawa@gmail.com\> \> wrote:
> > 
> > > But how do I know the weight of the received bytes ? I have think of  
> > > what you propose but can't find the way to "measure" the received stuff  
> > > !!!
> > > 
> > > I have to use readline or gets to receive because the server (a java  
> > > server) only offer me buffered stream and recv and read dont work in  
> > > this case...
> > 
> > &nbsp;&nbsp;It doesn't matter if the server is using a buffered stream, you'll  
> > get the bytes on the client anyway. Maybe something like this helps  
> > (haven't tried it):
> > 
> > data = nil  
> > left = nombre\_bytes  
> > while left \> 0  
> > &nbsp;&nbsp;# 8192 is an arbitrary buffer size  
> > &nbsp;&nbsp;buf = socket.read( left \> 8192 ? 8192 : left )  
> > &nbsp;&nbsp;# eof reached  
> > &nbsp;&nbsp;break unless buf
> > 
> > &nbsp;&nbsp;left -= buf.size  
> > &nbsp;&nbsp;data += buf  
> > end
> > 
> > @imageData = data
> > 
> > Why are you using a while to read return code, filename and size?  
> > Three socket.readline should be enough.
> > 
> > Bye
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
