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.
Example of 2 traditional messages on socket, that have come in just fine
using .each on the socket…
This would be two separate messages, such as:
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
BEAD,5,6,“Inside:2\r\n Outside:3\r\n Both:4\r\n”
@ascii = TCPSocket.new( IP, SOCKET)
@ascii_receive_thread = Thread.new do
@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…