Click here to Skip to main content
15,880,392 members
Articles / Programming Languages / C#
Article

Line Printer Daemon in C#

Rate me:
Please Sign up or sign in to vote.
4.42/5 (5 votes)
16 Jan 20073 min read 194.2K   4.2K   39   35
A line printer daemon in C#

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


Written By
Web Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWork only for 1 file Pin
Member 1486272114-Jun-20 12:44
Member 1486272114-Jun-20 12:44 
QuestionCopies Support Pin
Member 1291534625-Dec-16 9:41
Member 1291534625-Dec-16 9:41 
QuestionCould not get it working at all, please help!!! Pin
smitty168-Feb-16 2:24
smitty168-Feb-16 2:24 
QuestionHow to print pdf and BMP files? Pin
Ibrar Ahmad31-Jan-14 2:12
Ibrar Ahmad31-Jan-14 2:12 
GeneralSend character by LPR protocol Pin
vanhungit29-Mar-10 23:41
vanhungit29-Mar-10 23:41 
GeneralRe: Send character by LPR protocol Pin
rob tillaart30-Mar-10 1:36
rob tillaart30-Mar-10 1:36 
GeneralRe: Send character by LPR protocol Pin
vanhungit30-Mar-10 15:30
vanhungit30-Mar-10 15:30 
GeneralRe: Send character by LPR protocol Pin
rob tillaart31-Mar-10 0:06
rob tillaart31-Mar-10 0:06 
GeneralRe: Send character by LPR protocol Pin
vanhungit31-Mar-10 15:42
vanhungit31-Mar-10 15:42 
GeneralRe: Send character by LPR protocol Pin
rob tillaart31-Mar-10 21:06
rob tillaart31-Mar-10 21:06 
GeneralRe: Send character by LPR protocol Pin
vanhungit5-Apr-10 21:48
vanhungit5-Apr-10 21:48 
GeneralRe: Send character by LPR protocol Pin
rob tillaart5-Apr-10 22:43
rob tillaart5-Apr-10 22:43 
GeneralRe: Send character by LPR protocol Pin
vanhungit6-Apr-10 15:15
vanhungit6-Apr-10 15:15 
GeneralRe: Send character by LPR protocol Pin
rob tillaart6-Apr-10 21:08
rob tillaart6-Apr-10 21:08 
GeneralRe: Send character by LPR protocol [modified] Pin
rob tillaart6-Apr-10 22:00
rob tillaart6-Apr-10 22:00 
GeneralRe: Send character by LPR protocol Pin
vanhungit7-Apr-10 15:41
vanhungit7-Apr-10 15:41 
GeneralRe: Send character by LPR protocol Pin
rob tillaart7-Apr-10 22:06
rob tillaart7-Apr-10 22:06 
GeneralRe: Send character by LPR protocol Pin
vanhungit8-Apr-10 15:36
vanhungit8-Apr-10 15:36 
GeneralRe: Send character by LPR protocol Pin
rob tillaart8-Apr-10 20:47
rob tillaart8-Apr-10 20:47 
GeneralGreat job Pin
gstolarov3-Sep-09 5:56
gstolarov3-Sep-09 5:56 
General谢谢 Pin
ylwnet14-Mar-08 3:47
ylwnet14-Mar-08 3:47 
GeneralRun in WIndows service mode Pin
mswumei23-Feb-07 3:38
mswumei23-Feb-07 3:38 
GeneralStability Pin
mswumei21-Feb-07 7:17
mswumei21-Feb-07 7:17 
AnswerRe: Stability Pin
rob tillaart22-Feb-07 22:11
rob tillaart22-Feb-07 22:11 
GeneralRe: Stability Pin
mswumei23-Feb-07 2:35
mswumei23-Feb-07 2:35 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.