Skip to content

Instantly share code, notes, and snippets.

@rklemme
Created August 20, 2012 09:35
Show Gist options
  • Save rklemme/3402661 to your computer and use it in GitHub Desktop.
Save rklemme/3402661 to your computer and use it in GitHub Desktop.
Example of reading messages with a custom delimiter other than \n.
require 'socket'
DELIMITER = "\x04".freeze
server = TCPServer.new '127.0.0.1', 5556
printf "Server at port %p\n", server.addr
loop do
Thread.new(server.accept) do |client|
printf "Client %p START\n", client
client.each_line DELIMITER do |msg|
printf "msg len = %4d msg: %p bytes: %p\n", msg.length, msg, msg.unpack('C*').map {|i| "%02x" % i}
msg.chomp! DELIMITER
printf "msg len = %4d msg: %p bytes: %p\n", msg.length, msg, msg.unpack('C*').map {|i| "%02x" % i}
end
printf "Client %p STOP\n", client
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment