Pergunta

I'm using EventMachine tutorial as a starting point to experiment with sending / receiving binary data. The sample code is:

#!/usr/bin/env ruby

require 'rubygems' # or use Bundler.setup
require 'eventmachine'

class EchoServer < EM::Connection
  def receive_data(data)
    puts data
    send_data(data)
  end
end

EventMachine.run do
  # hit Control + C to stop
  Signal.trap("INT")  { EventMachine.stop }
  Signal.trap("TERM") { EventMachine.stop }

  EventMachine.start_server("0.0.0.0", 10000, EchoServer)
end

I use telent to connect to the EM server

telent -8 localhost:10000

I send the following data to the EM server:

\x17\xEB\xB3\b\x05\x00\x00\x00\x01\x00\x00\x89Bo\xAF

EM prints out this:

\\x17\\xEB\\xB3\\b\\x05\\x00\\x00\\x00\\x01\\x00\\x00\\x89Bo\\xAF\r\n

For some reason, it's escaping the slashes and adding \r\n to the received data.

How do I stop EM from escaping the data and just accept raw binary that's sent to it?

Foi útil?

Solução

Turns out the culprit is telnet. I solved this using netcat.

The command is from this question: How to escape hex values in netcat

echo "\x17\xEB\xB3\b\x05\x00\x00\x00\x01\x00\x00\x89Bo\xAF" | nc localhost 10000
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top