Created
August 20, 2012 09:35
-
-
Save rklemme/3402661 to your computer and use it in GitHub Desktop.
Example of reading messages with a custom delimiter other than \n.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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