# How to realize that a TCPSocket doesn't give any more lines as answer?

**URL:** <https://www.ruby-forum.com/t/how-to-realize-that-a-tcpsocket-doesnt-give-any-more-lines-as-answer/157242>\
**Category:** Ruby\
**Created:** [January 8, 2009, 8:15pm UTC](https://www.ruby-forum.com/t/how-to-realize-that-a-tcpsocket-doesnt-give-any-more-lines-as-answer/157242 "2009-01-08T20:15:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![saboteur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@saboteur](https://www.ruby-forum.com/u/saboteur)\
**Post date:** [January 8, 2009, 8:15pm UTC](https://www.ruby-forum.com/t/how-to-realize-that-a-tcpsocket-doesnt-give-any-more-lines-as-answer/157242/1 "2009-01-08T20:15:45Z")

</div>

Hi,  
I’m writing a little tcp-client which connects to a tcp-server, sends a  
command and recieves an answer. If I do something like that:

#!/usr/bin/env ruby

$VERBOSE=true

require ‘socket’  
…  
begin  
socket = TCPSocket.new($ip,$port)  
rescue  
puts “error: #{$!}”  
exit 1  
end

socket.print command

answer=socket.gets

socket.close

It works if the server sends just one line. But I don’t know how many  
lines the server answers. So I tried something like that:

answers=[]  
while answer=socket.gets  
answers\<\<answer  
end

But this results in the while-loop never ending. Even if just one line  
is the answer the while loop does simply never end ☹ Any idea how to do  
this?

---

<div class="post-metadata">

**Author:** ![saboteur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@saboteur](https://www.ruby-forum.com/u/saboteur)\
**Post date:** [January 8, 2009, 10:51pm UTC](https://www.ruby-forum.com/t/how-to-realize-that-a-tcpsocket-doesnt-give-any-more-lines-as-answer/157242/2 "2009-01-08T22:51:02Z")

</div>

removed\_email\_address@domain.invalid wrote:

> socket = TCPSocket.new($ip,$port)
> 
> It works if the server sends just one line. But I don’t know how many lines the server answers. So I tried something like that:
> 
> answers=[]  
> while answer=socket.gets  
> answers\<\<answer  
> end
> 
> But this results in the while-loop never ending. Even if just one line is the answer the while loop does simply never end ☹ Any idea how to do this?

Try socket.gets(nil) – that waits for EOF instead of newline.

---

<div class="post-metadata">

**Author:** ![saboteur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@saboteur](https://www.ruby-forum.com/u/saboteur)\
**Post date:** [January 9, 2009, 3:22pm UTC](https://www.ruby-forum.com/t/how-to-realize-that-a-tcpsocket-doesnt-give-any-more-lines-as-answer/157242/3 "2009-01-09T15:22:39Z")

</div>

2009/1/8 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> socket = TCPSocket.new($ip,$port)
> 
> ```
> 
> It works if the server sends just one line. But I don’t know how many lines the server answers. So I tried something like that:
> 
> answers=[]  
> while answer=socket.gets  
> answers\<\<answer  
> end
> 
> But this results in the while-loop never ending. Even if just one line is the answer the while loop does simply never end ☹ Any idea how to do this?

Basically you need to define a protocol of your own which handles  
this. There are many ways to do that. One option is to define that  
there will be only one line ever. Another option is to include  
something in each like which indicates whether there is more to  
follow. Yet another option is to define a terminator sequence and use  
gets or IO#each with terminator argument similar to this:

RKlemme@padrklemme1 ~  
$ cat \<\<EOF | ruby -e ‘ARGF.each(“EOM”) {|msg| p msg}’

> foo  
> bar  
> baz  
> EOM  
> dada  
> dodo  
> xxx  
> EOM  
> EOF  
> “foo\nbar\nbaz\nEOM”  
> “\ndada\ndodo\nxxx\nEOM”  
> “\n”

RKlemme@padrklemme1 ~  
$

Here “EOM” is the message terminator.

Or you can use DRb if all processes are Ruby processes. Then it’s  
handled by DRb and you just invoke methods.

Kind regards

robert
