From: Matt Brooks Date: 2011-01-11T05:54:49+09:00 Subject: reading off socket with \n, but a specific \n. 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 -- Posted via http://www.ruby-forum.com/.