reading off socket with \n, but a specific \n.

M

Matt Brooks

For a while my application has used, a TCPSocket and the .each method,
to continuously receive and populate an input buffer array with messages
that were simply \n terminated.

I have now run into a situation where I need to receive a message that
happens to have \n inside of the message in a string... I can not figure
out a way to make this work. Speed is a factor, I need this interface
as fast as possible, and in the past .each has been very fast. Early on
I used to read 1 byte at a time until receiving \n and then would add
that to buffer, but it proved a little too slow.

Any ideas? Thank you very much for your help.
-Matt


Example of 2 traditional messages on socket, that have come in just fine
using .each on the socket...
HEAD,2,1,2\nBEAD,5,6\n

This would be two separate messages, such as:
HEAD,2,1,2
BEAD,5,6



Now I have this situation....
HEAD,2,1,2\nBEAD,5,6,"Inside:2\r\n Outside:3\r\n Both:4\r\n"\n

I need the two messages separately, including the inside \r and \n, like
the following:
HEAD,2,1,2
BEAD,5,6,"Inside:2\r\n Outside:3\r\n Both:4\r\n"



######Example code:######

@ascii = TCPSocket.new( IP, SOCKET)

@ascii_receive_thread = Thread.new do
while(true)
@ascii.each do |ascii_line|
#Some code to put each message in an input buffer array... for use
elsewhere in program
#I also chomp off the newline character on the end...
end
end
 
P

Peter Vandenabeele

Matt Brooks wrote in post #973765:
For a while my application has used, a TCPSocket and the .each method,
to continuously receive and populate an input buffer array with messages
that were simply \n terminated.

I have now run into a situation where I need to receive a message that
happens to have \n inside of the message in a string... I can not figure
out a way to make this work. Speed is a factor, I need this interface
as fast as possible, and in the past .each has been very fast. Early on
I used to read 1 byte at a time until receiving \n and then would add
that to buffer, but it proved a little too slow.

Any ideas? Thank you very much for your help.
-Matt


Example of 2 traditional messages on socket, that have come in just fine
using .each on the socket...
HEAD,2,1,2\nBEAD,5,6\n

This would be two separate messages, such as:
HEAD,2,1,2
BEAD,5,6



Now I have this situation....
HEAD,2,1,2\nBEAD,5,6,"Inside:2\r\n Outside:3\r\n Both:4\r\n"\n

I need the two messages separately, including the inside \r and \n, like
the following:
HEAD,2,1,2
BEAD,5,6,"Inside:2\r\n Outside:3\r\n Both:4\r\n"

CSV libraries (e.g. the one in Ruby) have to resolve this same problem
of handling newlines inside quoted data blocks. You might look at how
they resolved this. When using rvm and 1.8.7 that would be the file:

~/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/csv.rb

Actually, if that is a CSV datastream, maybe you could use a standard
CSV library to parse it ?

HTH,

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top