Click here to Skip to main content
Click here to Skip to main content

Line Printer Daemon in C#

By , 16 Jan 2007
 

screenshot of the demo application

Introduction

This article presents a prototype of an LPD server written in C#. LPD stands for Line Printer Daemon. This class enables you to receive a print file sent by an LPR client. This can be an application, see An LPR client in C#, or the command line LPR of Linux or Windows. This class could be a basis for a print server that acts as a front-end for multiple printers, each printer defined by one queue.

Background

The LPR/LPD protocol is a 15+ year old print protocol from the TCP/IP suite that is still widely used in the area of network printers and print servers. It is described in detail in RFC 1179. Augmented variations of the protocol exist, like LPRNG also known as LPR Next Generation.

How it works

When the LPD server is started it reads the configuration file. After the start command it starts to listen on a port, default 515, for a connection. When connection is made by a (LPR)client the LPD server checks if the IP address of the client is allowed to send commands to the server. The core of this selection is an IPMatcher class, which checks if an ip address (IP4) is in a set of predefined ranges. If allowed the LPD server reads the first byte of the network stream to see what command is requested for. The command-set exist of:

  • RESTART: restart the processing of a queue
  • LPR: print a file
  • LPQ: request an overview of jobs in a queue
  • LPRM: removes a job from a queue

The LPD class currently supports the LPR and LPQ commands and accepts the RESTART and LPRM commands.

LPR

When the LPD server receives an LPR command, it checks if a directory associated with the requested queue exists, otherwise it is created. Then the control file and the data file is copied into this directory. The names are typically in the form CFAnnnNAME and DFAnnnNAME, C=control, D=Data, nnn = sequence number, and NAME is a hostname. Note that these names are defined by the LPR client. Further processing of the print file can be done by monitoring the queues/directories.

LPQ

When the LPD server receives an LPQ command, it first checks if the LPQ command is allowed. This is a configuration item that prevents users to see what others print over the network. The LPQ command comes in two flavours, long and short. The LPD class offers a simple implementation for both. If the requested queue does not exists an error message will returned.

LPRM

When the LPD server receives an LPRM command, the server checks if the command is allowed. If not it just acknowledges the request but doesn't do anything. If the command is allowed, the LPRM command should be executed but it is not implemented yet. Hook is ready.

RESTART

When the LPD server receives an RESTART command, the server acknowledges the request but doesn't do anything. If the command is allowed, the command should be executed but it is not implemented yet. Hook is ready.

Points of interest

Before starting the LPD application one must be sure that the Windows TCP/IP Print Service is stopped, as it performs a similar function. The interface of the class resembles a generic service to make it easier to embed the class in a Windows service. To process the LPR command correctly byte counting should be enabled in the TCP/IP port of the printer driver.

As the class is far from ready there are several things to improve. A short list in no specific order (no deadline).

  • improve LPR (support no byte counting)
  • implement LPRM
  • implement RESTART
  • improve LPQ
  • implement more error handling
  • implement backend filter
  • implement status and progress indicators - receiving/idle/%%
  • implement better logging
  • refactor ad fundum

History

  • 2007/01/13 - Version 1.00 - First version of LPD ready to publish.
  • 2007/01/09 - Version 0.00 - Getting started.

Usage rights

Everybody is granted to use this code as long as you refer to the original work, and I would appreciate that enhancements are published at CodeProject too.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

rob tillaart
Web Developer
Netherlands Netherlands
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralSend character by LPR protocolmembervanhungit29 Mar '10 - 23:41 
Hello rob tillaart,
 
I want to send character by LPR. I'm writing driver for Epson Printer but how to send command Printer and command LPR.
Can you help me? I need to send character in order to command printer by print server.
 
Thanks,
Hung
GeneralRe: Send character by LPR protocolmemberrob tillaart30 Mar '10 - 1:36 
Hi Hung,
 
Please take a look at An LPR client in C#[^] .
 
The democode can send a file to an device by means of the LPR/LPD protocol. You can create small files, eg. a file with codes to reset a printer, and send these to the printer or you could extend the interface of the class to allow a string to be send over the protocol. If you do the latter please let me know as I might include it in the demo code.
 
Hopes this helps you enough,
 
regards, Rob
GeneralRe: Send character by LPR protocolmembervanhungit30 Mar '10 - 15:30 
Hi Rob,
Thank for answering my question.
 
I saw your demo. At present, I can send a file (Text mode) to printer by means of the LPR/LPD protocol. But i don't know how to edit file include command Printer to print on Printer (EX: Epson Printer). I tryed editing content of file (include command to control Epson printer) but not active, I wonder whether this way is true? can you give idea?
 
Thanks,
Hung
GeneralRe: Send character by LPR protocolmemberrob tillaart31 Mar '10 - 0:06 
Hi Hung,
 
Quite some questions arise :
 
- What is the type of the Epson printer?
 
- Can you give examples of the characters you want to send to the printer and their purpose?
Are these "binary" escape sequences?
Where do you want to insert these codes? begin, end or anyplace within the file?
Or do you want to send commands to the printer between files.
(e.g. a reset command)
 
- Can you provide an (URL to the) specification of the Epson printer?
 
- Does the Epson printer print the textfiles correctly?
 
- Does the Epson printer support LPR directly or do you send the file via a printserver?
(and printer connected by USB, parallel to this server?)
 
- Does the Epson printer support command codes over TCP/IP?
Note: input-channels of printers sometimes differ in behavour.
 
Regards,
Rob
GeneralRe: Send character by LPR protocolmembervanhungit31 Mar '10 - 15:42 
Hi rob tillaart,
 
Thank for answering my question.
I want to send file to Epson TM-U220(Paralell gate) printer. I need to command (EPS/pos).(EX: reset, feed(LF), select font( ESC !),...)
I need to send textfile via a printserver and printer connected by parallel to this server.
 
Epson printer supports command codes over TCP/IP.
 
Thanks,
Hung
GeneralRe: Send character by LPR protocolmemberrob tillaart31 Mar '10 - 21:06 
Hi Hung,
 
According to http://www.icscale.com/TECH%20MANUALS/TMU220_TRG_RevD.pdf[^] the EPSON printer has a parallel interface or a TCP IP interface, but not both at the same time or am I incorrect.
 
I would make a copy of the textfile (e.g. COPY.TXT) to be printed and insert the ESC-codes in the proper place and then send COPY.TXT to the printer.
 
If this does not work try the following:
put the printer in het HEX mode to see exact context of whats received
2) send COPY.TXT to the printer over TCP/IP by means of my C# LPR code.
2) send COPY.TXT to the printer over TCP/IP by means of the windows LPR command.
3) send COPY.TXT directly to the printer over the parallel port. So skip the printserver completely.
 
If (3) doesn't work => there probably is something wrong with the file or the hardware of the printer
If (2) doesn't work and (3) does => the printserver is interfering somehow.
If (1) doesn't work and (2) and (3) does => there probably is a bug in my code.
 
Succes,
Rob
GeneralRe: Send character by LPR protocolmembervanhungit5 Apr '10 - 21:48 
Hi Rob,
 
I insert the ESC-codes in textfile to send printer but not work. I send string directly to the printer by pararell port (no printserver, no LPR protocol) is ok. I don't understand. can you help me?
 
Hung
GeneralRe: Send character by LPR protocolmemberrob tillaart5 Apr '10 - 22:43 
Hi Hung,
 
Test 3 = OK => The printer works
Test 2 = NOK => printerserver interferes somehow
 
Q1: Did you sent the textfile to the printserver with my LPR class or with the windows LPR application? Please try both and report results separately.
 
Q2: What are you using as printserver, Windows/Solaris/Linux/other?. Which version?
 
Q3: Can you halt the spooling of the printserver, so you can compare the sent file with the received file by the printserver. What are the results?
 
Regards,
Rob
GeneralRe: Send character by LPR protocolmembervanhungit6 Apr '10 - 15:15 
Hi Rob,
 
Thank you for answering my question.
 
I used Window OS. I send textfile to the printserver with your LPR class. I used to RFC 1179 (LPR protocol) but I don't find command of LPR which permit me insert command printer with filetext. Can you help me?
 
Thank, Hung
GeneralRe: Send character by LPR protocolmemberrob tillaart6 Apr '10 - 21:08 
Hi Hung,
 
There is no LPR command that permits inserting commands as far as I know. My class doesn't allow it either and I do not intend to extend it such way. The reason: LPR is a transportprotocol that does not change the content of the printfile (See RFC 1179). So your application should first create or modify the printfile before sending it by means of LPR. It is almost as simple as copying a file.
 
Your answers are very short and so questions remain:
 
Q1: You state that you sent the textfile with my LPR class. Did it work?
 
Q2: As asked before, have you tried the windows LPR application? It is a commandline tool. Start a dosbox and type LPR /? for explanation of the parameters. Please send the manually changed textfile through the commandline LPR to the printserver. What are the results?
 
Q3: Can you halt the spooling of the printserver, so you can compare the sent file with the received file by the printserver. What are the results?
 
Please send one answer per question.
 
Regards,
Rob

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 17 Jan 2007
Article Copyright 2007 by rob tillaart
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid