chumley has asked for the wisdom of the Perl Monks concerning the following question:

I need to redirect print output from an NT box to a Perl script running on Unix, where the text will be parsed and inserted into a database. Up to now I have been using a Lantronix device that outputs text over a TCP/IP connection. However, it's unreliable, and I'd like to set up a software-only connection.

I created a listener on the Unix server, that looks like this:

use IO::Socket; $sock = new IO::Socket::INET (LocalHost => 'UNIXhost', LocalPort => 3001, Proto => 'tcp', Listen => 5, Reuse => 1 ); die "Socket could not be created. Reason: $!\n" unless $sock; while ($new_sock = $sock->accept()) { while (defined ($buf = <$new_sock>)) { chomp ($buf); print "$buf\n"; } print "EOM\n\n"; } close ($sock);

(This is based on an example in "Advanced Perl Programming")

This works if I telnet to UNIXhost on port 3001 and type in some text.

I created a printer on the NT machine that uses a redirection to port 3001 on UNIXhost. The printer uses the Generic / Text Only driver supplied with Windows. When I submit a print job, the listener running on the Unix box prints "LPT1" and stops, and the print job hangs in NT. If I cancel the print job and submit a new one I see "LPT1" again on UNIXhost.

Is there something I should do to allow the socket to read the entire message? Is there a module that will allow a socket to emulate a print device? I looked through CPAN and the ActiveState docs and didn't find much help.

Thanks in advance,

Chumley

Replies are listed 'Best First'.
Re: Redirecting print output from WinNT to Unix
by kschwab (Vicar) on Mar 25, 2001 at 06:33 UTC
    Perhaps the output from the NT box isn't plain old line oriented text ? The way you are reading off the socket will only work if the NT box is sending newlines.

    You may want to try using read() to read bytes, instead of using the diamond operator.

    Another thought is to install Win NT's lpd client (this comes with NT, install from the Network app in the Control Panel).

    Once you've done that, you could configure your unix lpd daemon and create a queue that runs your script.

Re: Redirecting print output from WinNT to Unix
by a (Friar) on Mar 25, 2001 at 10:48 UTC
    Various suggestions: take a look at the perlcookbook (the code is on-line w/ O'Reilly) for a nicer socket, using read and an alarm timeout. Try undefing $/ so you get slurp mode? Make sure the printer on NT will print normally (odd it would send the name of the lpt port as its first word, if it was expecting to be talking w/ a, say, ascii printer) when not redirecting to a remote 3001 port. Look at Net::Daemon (though I think the trouble is w/ NT). Maybe you need to ack somehow and that's what the printer is waiting for ...

    a

Re: Redirecting print output from WinNT to Unix
by t0mas (Priest) on Mar 26, 2001 at 01:20 UTC
    One option you have is to use the Redmon program that redirects a Windows printer port to a program (usually Ghostscript). You could then write a Perl program on the Windows box (that gets the Windows LPT output) and make it talk to the Perl program on the Unix box through a socket, and thus have control in both ends.

    /brother t0mas

      Aha... this could be exactly what I need. It will also give me the option of writing a general-purpose server component that I could connect to from various clients, in case I need to do something similar elsewhere.

      Thanks, Brother t

      Chumley