Click here to Skip to main content
15,889,335 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.5K   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

 
GeneralBinary mode. Pin
mswumei21-Feb-07 6:32
mswumei21-Feb-07 6:32 
AnswerRe: Binary mode. Pin
rob tillaart22-Feb-07 23:00
rob tillaart22-Feb-07 23:00 
Generalimplemnting this function Pin
candarins7-Feb-07 19:05
candarins7-Feb-07 19:05 
GeneralRe: implemnting this function Pin
rob tillaart17-Feb-07 5:18
rob tillaart17-Feb-07 5:18 
QuestionWhere is the Code of this project Pin
Samuel 8219-Jan-07 2:10
Samuel 8219-Jan-07 2:10 
AnswerRe: Where is the Code of this project Pin
Paul Conrad19-Jan-07 4:18
professionalPaul Conrad19-Jan-07 4:18 
AnswerRe: Where is the Code of this project Pin
Nish Nishant19-Jan-07 4:46
sitebuilderNish Nishant19-Jan-07 4:46 
GeneralMissin' Source Pin
NormDroid16-Jan-07 21:22
professionalNormDroid16-Jan-07 21:22 
GeneralRe: Missin' Source Pin
Paul Conrad19-Jan-07 4:18
professionalPaul Conrad19-Jan-07 4:18 
GeneralRe: Missin' Source Pin
NormDroid19-Jan-07 4:42
professionalNormDroid19-Jan-07 4:42 

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.