# TCPSocket Flushing

**URL:** <https://rubytalk.org/t/tcpsocket-flushing/39852>\
**Category:** ruby-talk\
**Created:** [13 August 2007 03:33 UTC](https://rubytalk.org/t/tcpsocket-flushing/39852 "2007-08-13T03:33:53Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![David10](https://avatars.discourse-cdn.com/v4/letter/d/c4cdca/32.png) [@David10](https://rubytalk.org/u/David10)\
**Post date:** [13 August 2007 03:33 UTC](https://rubytalk.org/t/tcpsocket-flushing/39852/1 "2007-08-13T03:33:53Z")

</div>

I have two programs, a server and a client. I want to send two messages  
to the client when the client connects to the server, with a delay in  
between. However, both messages seem to arrive at the client at the same  
time. Here are the programs:

## simpleserver.rb  
## run this first  
require "socket"  
server = TCPServer.new(2000)  
while connection = server.accept  
&nbsp;&nbsp;connection.write "one\n"  
&nbsp;&nbsp;connection.flush  
&nbsp;&nbsp;sleep 1  
&nbsp;&nbsp;connection.write "two\n"  
&nbsp;&nbsp;connection.close  
end

## client.rb  
## run this second  
require "socket"  
sock = TCPSocket.open("localhost", 2000)  
while not sock.eof?  
&nbsp;&nbsp;print sock.read  
end

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

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [13 August 2007 11:52 UTC](https://rubytalk.org/t/tcpsocket-flushing/39852/2 "2007-08-13T11:52:56Z")

</div>

As far as I can see you read the whole stream in one go with  
sock.read. Try this instead

sock.each\_line do |line|  
&nbsp;&nbsp;puts line  
end

Kind regards

robert

> **···**
>
> 2007/8/13, David -- \<neko18@gmail.com\>:
> 
> > I have two programs, a server and a client. I want to send two messages  
> > to the client when the client connects to the server, with a delay in  
> > between. However, both messages seem to arrive at the client at the same  
> > time. Here are the programs:
> > 
> > ## simpleserver.rb  
> > ## run this first  
> > require "socket"  
> > server = TCPServer.new(2000)  
> > while connection = server.accept  
> > &nbsp;&nbsp;connection.write "one\n"  
> > &nbsp;&nbsp;connection.flush  
> > &nbsp;&nbsp;sleep 1  
> > &nbsp;&nbsp;connection.write "two\n"  
> > &nbsp;&nbsp;connection.close  
> > end
> > 
> > ## client.rb  
> > ## run this second  
> > require "socket"  
> > sock = TCPSocket.open("localhost", 2000)  
> > while not sock.eof?  
> > &nbsp;&nbsp;print sock.read  
> > end

---

<div class="post-metadata">

**Author:** ![See](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@See](https://rubytalk.org/u/See)\
**Post date:** [13 August 2007 14:34 UTC](https://rubytalk.org/t/tcpsocket-flushing/39852/3 "2007-08-13T14:34:57Z")

</div>

In article \<18e16d93f4d0a9e65517e49c497bcfcd@ruby-forum.com\>,  
&nbsp;&nbsp;David -- \<neko18@gmail.com\> writes:

> I have two programs, a server and a client. I want to send two messages  
> to the client when the client connects to the server, with a delay in  
> between. However, both messages seem to arrive at the client at the same  
> time. Here are the programs:
> 
> ## simpleserver.rb  
> ## run this first  
> require "socket"  
> server = TCPServer.new(2000)  
> while connection = server.accept  
> &nbsp;&nbsp;connection.write "one\n"  
> &nbsp;&nbsp;connection.flush  
> &nbsp;&nbsp;sleep 1  
> &nbsp;&nbsp;connection.write "two\n"  
> &nbsp;&nbsp;connection.close  
> end
> 
> ## client.rb  
> ## run this second  
> require "socket"  
> sock = TCPSocket.open("localhost", 2000)  
> while not sock.eof?  
> &nbsp;&nbsp;print sock.read  
> end

If the format of your server "messages" is \*always\* lines, i.e.,  
strings terminated by new-lines and/or carriage-return/line-feeds,  
then you should be able to just replace the read with a gets,  
e.g.:

&nbsp;&nbsp;&nbsp;&nbsp;require "socket"  
&nbsp;&nbsp;&nbsp;&nbsp;sock = TCPSocket.open("localhost", 2000)  
&nbsp;&nbsp;&nbsp;&nbsp;while not sock.eof?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print sock.gets  
&nbsp;&nbsp;&nbsp;&nbsp;end

If that is not the case, you will need to use something like the  
following technique:

&nbsp;&nbsp;&nbsp;&nbsp;require "socket"  
&nbsp;&nbsp;&nbsp;&nbsp;sock = TCPSocket.open("localhost", 2000)  
&nbsp;&nbsp;&nbsp;&nbsp;loop do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str = sock.recv(100)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break if str.length == 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print str  
&nbsp;&nbsp;&nbsp;&nbsp;end

I have encountered the same problem in the past and I have looked  
in vain for any documentation on the subject. So, the following  
is pure supposition on my part, but it reflects my experience in  
using sockets in Ruby:

I think that this is due to the magic of sockets in Ruby: you can  
use a socket as either a socket object or as an IO object. Once  
you use it as an IO object, however, it effectively becomes an IO  
object and certain socket operations can no longer be used. So,  
once you use either the eof? or read operations, the socket switches  
to IO object mode, (i.e., buffered data) and the read call will block  
until either the entire read is complete or an EOF occurs. Since  
you didn't specify any size of the read operation, it blocks until  
it has read the entire "file." Even if you had specified a size  
to the read operation, it would have blocked until an entire buffer  
of that size was filled (or EOF). Limiting the operations to those  
available for sockets (e.g., recv) avoids this problem.

- dmw

> **···**
>
> --  
> . Douglas Wells . Connection Technologies .  
> . Internet: -sp9804- -at - contek.com- .

---

<div class="post-metadata">

**Author:** ![Francis\_Cianfrocca](https://avatars.discourse-cdn.com/v4/letter/f/7bcc69/32.png) [@Francis\_Cianfrocca](https://rubytalk.org/u/Francis_Cianfrocca)\
**Post date:** [15 August 2007 06:38 UTC](https://rubytalk.org/t/tcpsocket-flushing/39852/4 "2007-08-15T06:38:46Z")

</div>

Did you try using IO#close\_write?

The solution of reading the input in the client works well enough if you  
know the data is line-structured, and if you have a positive way of knowing  
when you've read it all. Sometimes you have to deal with a client that just  
reads the input until it jams. (Programs intended to be used in Unix  
pipelines often work this way.) In these cases, it can help to call  
IO#close\_write on the server side. That causes IO#read in the client to  
return.

Someone recently did put up a Wiki-version of the documentation for Ruby and  
a big pile of the public Rubygems. Looked pretty good but I don't know if  
it's being successful.

> **···**
>
> n 8/12/07, David -- \<neko18@gmail.com\> wrote:
> 
> > I have two programs, a server and a client. I want to send two messages  
> > to the client when the client connects to the server, with a delay in  
> > between. However, both messages seem to arrive at the client at the same  
> > time. Here are the programs:
> > 
> > ## simpleserver.rb  
> > ## run this first  
> > require "socket"  
> > server = TCPServer.new(2000)  
> > while connection = server.accept  
> > &nbsp;&nbsp;connection.write "one\n"  
> > &nbsp;&nbsp;connection.flush  
> > &nbsp;&nbsp;sleep 1  
> > &nbsp;&nbsp;connection.write "two\n"  
> > &nbsp;&nbsp;connection.close  
> > end
> > 
> > ## client.rb  
> > ## run this second  
> > require "socket"  
> > sock = TCPSocket.open("localhost", 2000)  
> > while not sock.eof?  
> > &nbsp;&nbsp;print sock.read  
> > end  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![David10](https://avatars.discourse-cdn.com/v4/letter/d/c4cdca/32.png) [@David10](https://rubytalk.org/u/David10)\
**Post date:** [13 August 2007 14:31 UTC](https://rubytalk.org/t/tcpsocket-flushing/39852/5 "2007-08-13T14:31:01Z")

</div>

> As far as I can see you read the whole stream in one go with  
> sock.read. Try this instead
> 
> sock.each\_line do |line|  
> &nbsp;&nbsp;puts line  
> end
> 
> Kind regards
> 
> robert

It worked perfectly, thank you.

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

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [15 August 2007 02:32 UTC](https://rubytalk.org/t/tcpsocket-flushing/39852/6 "2007-08-15T02:32:35Z")

</div>

Yeah try using socket.recv(1024) or what not. I don't know what read  
does--it might wait until it closes to it can read the entire thing  
'once' --as stated typically read (with files) reads the  
entire file into one large string--not sure if it does so here, or tries  
to, as well,  
or not. Good luck.  
-Roger  
Belief is good. [free bible - Google Search](http://www.google.com/search?q=free+bible)  
Douglas Wells wrote:

> **···**
>
> > In article \<18e16d93f4d0a9e65517e49c497bcfcd@ruby-forum.com\>,  
> > &nbsp;&nbsp;David -- \<neko18@gmail.com\> writes:
> > 
> > > &nbsp;&nbsp;connection.write "one\n"  
> > > while not sock.eof?  
> > > &nbsp;&nbsp;print sock.read  
> > > end
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![David10](https://avatars.discourse-cdn.com/v4/letter/d/c4cdca/32.png) [@David10](https://rubytalk.org/u/David10)\
**Post date:** [15 August 2007 02:43 UTC](https://rubytalk.org/t/tcpsocket-flushing/39852/7 "2007-08-15T02:43:58Z")

</div>

Ruby seems to be lacking in its documentation of sockets, unless I'm  
just looking in the wrong places. I hadn't seen anything about the  
Socket::recv method. Thanks for all the help though, everyone.

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

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [15 August 2007 04:46 UTC](https://rubytalk.org/t/tcpsocket-flushing/39852/8 "2007-08-15T04:46:01Z")

</div>

I agree--I wish Ruby had a comprehensive documentation+wiki (or  
documentation with comments). Anyone want to help with that one? 🙂  
-Roger

David -- wrote:

> **···**
>
> > Ruby seems to be lacking in its documentation of sockets, unless I'm  
> > just looking in the wrong places. I hadn't seen anything about the  
> > Socket::recv method. Thanks for all the help though, everyone.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [21 August 2007 18:11 UTC](https://rubytalk.org/t/tcpsocket-flushing/39852/9 "2007-08-21T18:11:53Z")

</div>

My question is: what is the 'write' equivalent of 'recv' (i.e. write non  
blocking) for sockets...hmm...

David -- wrote:

> **···**
>
> > Ruby seems to be lacking in its documentation of sockets, unless I'm  
> > just looking in the wrong places. I hadn't seen anything about the  
> > Socket::recv method. Thanks for all the help though, everyone.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [15 August 2007 05:57 UTC](https://rubytalk.org/t/tcpsocket-flushing/39852/10 "2007-08-15T05:57:10Z")

</div>

See ruby-doc.org and the mailing list at ruby-doc@ruby-lang.org.

The best way to contribute to documentation is submit RDoc patches.

> **···**
>
> On Aug 14, 2007, at 21:46, Roger Pack wrote:
> 
> > David -- wrote:
> > 
> > > Ruby seems to be lacking in its documentation of sockets, unless I'm  
> > > just looking in the wrong places. I hadn't seen anything about the  
> > > Socket::recv method. Thanks for all the help though, everyone.
> > 
> > I agree--I wish Ruby had a comprehensive documentation+wiki (or  
> > documentation with comments). Anyone want to help with that one? 🙂
> 
> --  
> Poor workers blame their tools. Good workers build better tools. The  
> best workers get their tools to do the work for them. -- Syndicate Wars

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [15 August 2007 18:24 UTC](https://rubytalk.org/t/tcpsocket-flushing/39852/11 "2007-08-15T18:24:58Z")

</div>

The pickaxe is pretty comprehensive and has good examples IMHO.

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 15.08.2007 06:46, Roger Pack wrote:
> 
> > I agree--I wish Ruby had a comprehensive documentation+wiki (or documentation with comments). Anyone want to help with that one? 🙂

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [22 August 2007 07:56 UTC](https://rubytalk.org/t/tcpsocket-flushing/39852/12 "2007-08-22T07:56:12Z")

</div>

I believe the pairing is read-write and send-recv.

robert

> **···**
>
> 2007/8/21, Roger Pack \<rogerpack2005@gmail.com\>:
> 
> > My question is: what is the 'write' equivalent of 'recv' (i.e. write non  
> > blocking) for sockets...hmm...

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [22 August 2007 18:53 UTC](https://rubytalk.org/t/tcpsocket-flushing/39852/13 "2007-08-22T18:53:40Z")

</div>

Thanks!  
Robert Klemme wrote:

> **···**
>
> > 2007/8/21, Roger Pack \<rogerpack2005@gmail.com\>:
> > 
> > > My question is: what is the 'write' equivalent of 'recv' (i.e. write non  
> > > blocking) for sockets...hmm...
> > 
> > I believe the pairing is read-write and send-recv.
> > 
> > robert
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
