Search code examples
ruby-on-railsrubysocketsprintingtelnet

Ruby: Connecting to printer using ruby


I am trying to connect to printer using ruby language. Printer vendor has given use certain commands that i can use to communicate with printer.

I am using ruby telnet library to communicate with printer. it is working fine. Below is the code that is working for me using ruby telnet library.

require 'net/telnet'
localhost = Net::Telnet::new("Host" => "192.168.25.168","Port" => 20000, "Timeout" => 15)
localhost.cmd("MARK START") # starts printer

The above code works perfectly but i don't want to use telent library. i want to execute printer commands using ruby socket programming.

I have tried below code but it doesn't work

require 'socket'
sock = TCPSocket.new('192.168.25.168', 20000)
sock.write 'MARK START'
sock.close

What could be the reason for this?? why code with telnet library is working and code with TCP socket programming not working.


Solution

  • It seems that the printer is using Telnet protocol, described here. Telnet sits at a higher level of abstraction and uses TCP to transmit it's data. You are trying to send messages over tcp without any telnet connection being established while your printer expects a telnet connection, in order to receive commands.