Encoding with TCPSocket

Hello!

I’d like to monitor the status of a lpr-Printer with ruby. I found nice
snippet of Java Code that works fine but I’m running into problems
translating it to ruby.
Obviously my problems have to to with byte-encoding…
The Java-Code is using a socket and sending the data as a byte-Array:


Socket socket = new Socket(ip,port);
socket.getOutputStream().write(" List\n".getBytes());

Like already said this works just like a charm…
My ruby-Snippet looks like this:

socket = TCPSocket::new(server,port)
socker.print(" List\n")

socket.flush
respone=socket.gets

Unfortunately all I get are some cryptic characters…

Can somebody give me a hint how to do this in ruby?

Michael

Unicode? You probably have to convert it to ASCII before printing it on
screen. Take a look at the iconv module.

Regards,

  Michael

···

On Fri, May 07, 2004 at 07:43:55PM +0900, Michael Mueller wrote:

Hello!

I'd like to monitor the status of a lpr-Printer with ruby. I found nice
snippet of Java Code that works fine but I'm running into problems
translating it to ruby.
Obviously my problems have to to with byte-encoding...
The Java-Code is using a socket and sending the data as a byte-Array:

....
Socket socket = new Socket(ip,port);
socket.getOutputStream().write(" List\n".getBytes());
....

Like already said this works just like a charm...
My ruby-Snippet looks like this:
....
socket = TCPSocket::new(server,port)
socker.print(" List\n")
....
socket.flush
respone=socket.gets
...

Unfortunately all I get are some cryptic characters...

Can somebody give me a hint how to do this in ruby?

Does it look exactly like this? There is a typo in line two, you
spelled socket wrong.

Otherwise, have you tried manually telnetting to that host:port? Type
in “List”, hit return, and see what happens. (I think any OS nowadays
comes with a telnet app, right?)

If I had an lpr server, I’d check it, but I don’t. :slight_smile:

HTH,
–Mark

···

On May 7, 2004, at 3:43 AM, Michael Mueller wrote:

My ruby-Snippet looks like this:

socket = TCPSocket::new(server,port)
socker.print(" List\n")

socket.flush
respone=socket.gets

Wrote Michael Neumann mneumann@ntecs.de, on Fri, May 07, 2004 at 07:53:39PM +0900:

Hello!

I’d like to monitor the status of a lpr-Printer with ruby. I found nice
snippet of Java Code that works fine but I’m running into problems
translating it to ruby.
Obviously my problems have to to with byte-encoding…
The Java-Code is using a socket and sending the data as a byte-Array:


Socket socket = new Socket(ip,port);
socket.getOutputStream().write(" List\n".getBytes());

Like already said this works just like a charm…
My ruby-Snippet looks like this:

socket = TCPSocket::new(server,port)
socker.print(" List\n")

socket.flush
respone=socket.gets

Unfortunately all I get are some cryptic characters…

At least you’re getting something…

What are the cryptic characters?

Can somebody give me a hint how to do this in ruby?

Unicode? You probably have to convert it to ASCII before printing it on
screen. Take a look at the iconv module.

I’d be pretty surprised if the lpr protocols used 16bit unicode! They
should all be ascii. But, what is the protocol being used to talk to
the “lpr-Printer”, maybe it’s not an lpd printer?

Sam

···

On Fri, May 07, 2004 at 07:43:55PM +0900, Michael Mueller wrote:


Sam Roberts sroberts@certicom.com

Michael Neumann wrote:

Unicode? You probably have to convert it to ASCII before printing it on
screen. Take a look at the iconv module.

I don’t think so… all I get is one char…

In the Java-Example the printer responds something like
“There are currently no jobs in the queue” (or similar)…

Regards,
Michael

At least you’re getting something…

Lucky me :wink:

What are the cryptic characters?

Like I said in the response to Michael it’s actually only one character…

I’d be pretty surprised if the lpr protocols used 16bit unicode! They
should all be ascii. But, what is the protocol being used to talk to
the “lpr-Printer”, maybe it’s not an lpd printer?

I don’t think either that it’s unicode…
The Java-Snippet makes no conversion at all (besides the String#toByte).

Regards,
Michael