# Quick question on reading from TCPSockets

**URL:** <https://rubytalk.org/t/quick-question-on-reading-from-tcpsockets/54353>\
**Category:** ruby-talk\
**Created:** [10 July 2009 22:15 UTC](https://rubytalk.org/t/quick-question-on-reading-from-tcpsockets/54353 "2009-07-10T22:15:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Greg\_Chambers](https://avatars.discourse-cdn.com/v4/letter/g/b9bd4f/32.png) [@Greg\_Chambers](https://rubytalk.org/u/Greg_Chambers)\
**Post date:** [10 July 2009 22:15 UTC](https://rubytalk.org/t/quick-question-on-reading-from-tcpsockets/54353/1 "2009-07-10T22:15:02Z")

</div>

So in my program, I am reading a series of ASCII strings from the  
TCPSocket. My code is letting everything through just fine. The  
problem is in the verification of the data. You see, the point of this  
protocol I am using is I am receiving a series of strings where each  
string has a 4 letter command and then parameters after it. The command  
and parameters are separated by commas. So my main worry here is most  
of the examples of TCPSocket networking I've seen use the #read method  
which requires a known amount of bytes to be read and I am worried that  
I will cut one of the commands part way through and cause problems.

I thought about using #readlines but I wasn't sure how it would treat a  
split message. If I get a message like

> GHWD,12,67585,234,4767,234232,757  
> HGYT,0,0  
> PJIY,9876,234,56432,23

but the full last command was actually

> PJIY,9876,234,56432,234565,234,7774

then that messes everything up. Is there a way to read data delimited  
by a newline and if the last line read does not end with a newline to  
have it placed back in a buffer of some sort to get the next set of  
messages appended to it?

> **···**
>
> --  
> 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:** [10 July 2009 23:16 UTC](https://rubytalk.org/t/quick-question-on-reading-from-tcpsockets/54353/2 "2009-07-10T23:16:03Z")

</div>

> then that messes everything up. Is there a way to read data delimited  
> by a newline and if the last line read does not end with a newline to  
> have it placed back in a buffer of some sort to get the next set of  
> messages appended to it?

Yeah you'd have to buffer it and process it only when a newline comes  
in.  
=r

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

---

<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:** [11 July 2009 07:55 UTC](https://rubytalk.org/t/quick-question-on-reading-from-tcpsockets/54353/3 "2009-07-11T07:55:06Z")

</div>

You can simply use #gets or #each (same as #each\_line). Note that #each and #each\_line accept separators so in case you have something different as separator character for records (commands) you can still use that approach. You need to be aware though that the separator character is preserved:

socket.each "\n" do |command|  
&nbsp;&nbsp;&nbsp;command.chomp!  
&nbsp;&nbsp;&nbsp;cmd, \*args = command.split ','  
end

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 11.07.2009 00:15, Greg Chambers wrote:
> 
> > So in my program, I am reading a series of ASCII strings from the  
> > TCPSocket. My code is letting everything through just fine. The  
> > problem is in the verification of the data. You see, the point of this  
> > protocol I am using is I am receiving a series of strings where each  
> > string has a 4 letter command and then parameters after it. The command  
> > and parameters are separated by commas. So my main worry here is most  
> > of the examples of TCPSocket networking I've seen use the #read method  
> > which requires a known amount of bytes to be read and I am worried that  
> > I will cut one of the commands part way through and cause problems.
> > 
> > I thought about using #readlines but I wasn't sure how it would treat a  
> > split message. If I get a message like
> > 
> > > GHWD,12,67585,234,4767,234232,757  
> > > HGYT,0,0  
> > > PJIY,9876,234,56432,23
> > 
> > but the full last command was actually
> > 
> > > PJIY,9876,234,56432,234565,234,7774
> > 
> > then that messes everything up. Is there a way to read data delimited  
> > by a newline and if the last line read does not end with a newline to  
> > have it placed back in a buffer of some sort to get the next set of  
> > messages appended to it?
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
