Subject: | |
From: | |
Reply To: | |
Date: | Fri, 7 Jul 2000 08:45:02 -0700 |
Content-Type: | text/plain |
Parts/Attachments: |
|
|
Hi HP3000-L,
How could I resist submitting another Perl entry to the language wars? ;-)
The following Perl script corresponds almost exactly statement-by-statement to
Lars' Java original. My script uses the "new" object-style sockets interface
in IO::Socket, but the old procedural sockets calls are available too, so I
could have written this to look more like the longer C example posted by Kevin
Miller.
#!/PERL/PUB/perl -w
# Specify any external modules.
use IO::Socket;
# Set parameter defaults.
$port = 9100;
$next = 1;
# Parse the parameters.
if ($#ARGV >= 0) { $port = $ARGV[0] };
if ($#ARGV >= 1) { $next = $ARGV[1] };
# Initialize the server socket.
$serv = IO::Socket::INET->new(Listen => 2,
Proto => 'tcp',
LocalPort => $port) or die "socket() failed: $!";
# Loop forever waiting to accept new connections.
while (1) {
printf "FakeLP listener ready\n";
# Wait for a new connection.
$client = $serv->accept() or die "accept() failed: $!";
# Generate the new output filename.
$name = 'F'.$next++;
printf "Capturing spoolfile to %s\n",$name;
# Create the output file and copy all client data to it.
open(FO,">$name") or die "open() failed: $!";
while (<$client>) { print FO };
close(FO) or die "close on $name failed: $!";
# Close the client socket.
$client->close;
}
- Mark B.
Lars Appel wrote:
> // FakeLP pretends network printer to capture spooler PCL output
>
> import java.net.*;
> import java.io.*;
>
> class FakeLP {
>
> public static void main( String args[] ) throws Exception {
>
> int port = 9100;
> int next = 1;
>
> if (args.length > 0) port = Integer.parseInt(args[0]);
> if (args.length > 1) next = Integer.parseInt(args[1]);
>
> ServerSocket serv = new ServerSocket( port );
>
> while (true) {
>
> System.out.println("FakeLP listener ready");
>
> Socket sock = serv.accept();
> byte[] buf = new byte[4096];
> String name = "F" + (next++);
>
> System.out.println("Capturing spoolfile to " + name);
>
> InputStream si = sock.getInputStream();
> OutputStream fo = new FileOutputStream(name);
>
> for (;;)
> {
> int got = si.read(buf);
>
> if (got != -1)
> fo.write(buf, 0, got);
> else
> break;
> }
>
> fo.close();
> si.close();
> }
> }
> }
|
|
|