You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
855 B

require 'date'
require 'time'
require 'socket'
DATEFMT = '%Y%m%d%H%M%S'
def recordify(msg)
code = '%-31s' % 'DERP'
num = '%-12d' % (rand.to_s.tr('.', '0')[0,5]).to_i
timestamp = Time.now.strftime(DATEFMT)
len = '%-6d' % msg.length
"#{code}#{num}#{len}#{timestamp}#{msg.gsub(/\r/, '\r').gsub(/\n/, '\n')}\r\n"
end
def main
print "What to say?: "
msg = gets
TCPSocket.open('127.0.0.1', 15778) do |sock|
sock.send(recordify(msg), 0)
response = sock.recv(66)
sock.close
puts "raw response --> #{response.inspect}"
code = response[0,31].to_s.strip
num = response[31,12].to_s.strip
raw_timestamp = response[49,14].to_s.strip
timestamp = DateTime.strptime(raw_timestamp, DATEFMT)
puts "code: '#{code}'"
puts "num: '#{num}'"
puts "timestamp: '#{timestamp}'"
end
end
if __FILE__ == $0
main
end