Network Programming with Perl / Edition 1

Paperback (Print)
Used and New from Other Sellers
Used and New from Other Sellers
from $1.99
Usually ships in 1-2 business days
(Save 96%)
Other sellers (Paperback)
  • All (20) from $1.99   
  • New (6) from $43.37   
  • Used (14) from $1.99   

Overview

"This is a superb book. Another splendid book from Lincoln, whose mastery and lucid exposition make this a must-have for the serious Perl programmer."
--Jon Orwant, Chief Technology Officer, O'Reilly & Associates
Founder of The Perl Journal, author of Mastering Algorithms with Perl, (O'Reilly & Associates)
and co-author of Programming Perl, Third Edition (O'Reilly & Associates)

Network Programming with Perl is a comprehensive, example-rich guide to creating network-based applications using the Perl programming language. Among its many capabilities, modern Perl provides a straightforward and powerful interface to TCP/IP, and this book shows you how to leverage these capabilities to create robust, maintainable, and efficient custom client/server applications.

The book quickly moves beyond the basics to focus on high-level, application programming concepts, tools, and techniques. Readers will find a review of basic networking concepts and Perl fundamentals, including Perl's I/O functions, process model, and object-oriented extensions. In addition, the book examines a collection of the best third-party modules in the Comprehensive Perl Archive Network, including existing network protocols for e-mail, news, and the Web.

The core of the book focuses on methods and alternatives for designing TCP-based client/server systems and more advanced techniques for specialized applications. Specific topics covered include:

  • The Berkeley Sockets API
  • The TCP protocol and the IO::Socket API
  • FTP filesharing service
  • The Net::Telnet module for adapting clients to interactive network services
  • SMTP, including how to create and send e-mails with multimedia attachments
  • POP, IMAP, and NNTP for receiving and processing e-mail
  • HTTP and the LWP module for communicating with Web servers
  • Forking servers and the UNIX and Windows inetd daemons
  • Perl's experimental multithreaded API
  • Multiplexed operations and nonblocking I/O
  • Bulletproofing servers
  • TCP urgent data
  • UDP protocol and servers
  • Broadcasting and multicasting
  • Interprocess communication with UNIX-domain sockets

Useful, working programs demonstrate ideas and techniques in action, including a real-time chat and messaging system, a program for processing e-mail containing MIME attachments, a program for mirroring an FTP site, and a Web robot.

Network Programming with Perl focuses on TCP/IP rather than just the common Web protocols. Modeled after the critically acclaimed TCP/IP Illustrated by W. Richard Stevens, this book achieves a level of detail far superior to most. It is an essential resource for network administrators and Perl programmers who are creating network applications.

0201615711B04062001

Read More Show Less

Editorial Reviews

Gregory V. Wilson
Network Programming with Perl is clear, accurate, well organized, and comprehensive without being exhausting. After a five-chapter introduction to basic concepts, such as Perl I/O and the TCP protocol, Stein explains how to develop clients for common services such as FTP, Telnet, SMTP, POP, and so on. The third part of the book then looks at TCP-based client/server systems, with chapters on forking servers, multithreading, multiplexing, and nonblocking I/O. The last part covers advanced topics, including urgent data, UDP, broadcasting, and multicasting.

Every chapter has lots of short examples, many of which would be good starting points for real applications. What's even better, Stein takes the time to explain why things ought to be done certain ways, and which problems can be avoided by using which techniques.
Electronic Review of Computer Books

Read More Show Less

Product Details

  • ISBN-13: 9780201615715
  • Publisher: Addison-Wesley
  • Publication date: 12/29/2000
  • Edition description: New Edition
  • Edition number: 1
  • Pages: 784
  • Product dimensions: 7.40 (w) x 9.10 (h) x 1.80 (d)

Meet the Author

Lincoln Stein has an M.D. and is a scientist at Cold Spring Harbor Laboratory. When the Web first emerged, he created and maintained one of the earliest Internet sites for distribution of Human Genome Project data and has since become an acknowledged expert in Web, network, and Perl programming. Known for his exceptional ability to synthesize and present complex information, he writes for The Perl Journal and Web Techniques magazines and is the author of four other books.

0201615711AB04062001

Read More Show Less

Read an Excerpt

The network is everywhere. At the office, machines are wired together into local area networks, and the local networks are interconnected via the Internet. At home, personal computers are either intermittently connected to the Internet, or, increasingly, "always-on" cable and DSL modems. New wireless technologies, such as Bluetooth, promise to vastly expand the network realm, embracing everything from cell phones to kitchen appliances.

Such an environment creates tremendous opportunities for innovation. Whole new classes of applications are now predicated on the availability of high-bandwidth, always-on connectivity. Interactive games allow players from around the globe to compete on virtual playing fields and the instant messaging protocols let them broadcast news of their triumphs to their friends. New peer-to-peer systems, such as Napster and Gnutella, allow people to directly exchange MP3 audio files and other types of digital content. The SETI@Home project takes advantage of idle time on the millions of personal computers around the world to search for signs of extraterrestrial life in a vast collection of cosmic noise.

The ubiquity of the network allows for more earthbound applications as well. With the right knowledge, you can write a robot that will fetch and summarize prices from competitors' Web sites; a script to page you when a certain stock drops below a specified level; a program to generate daily management reports and send them off via e-mail; a server that centralizes some number-crunching task on a single high-powered machine, or alternatively distributes that task among the multiple nodes of a computer cluster.

Whether you are searching for the best price on a futon or for life in a distant galaxy, you'll need to understand how network applications work in order to take full advantage of these opportunities. You'll need a working understanding of the TCP/IP protocol—the common denominator for all Internet-based communications and the most common protocol in use in local area networks as well. You'll need to know how to connect to a remote program, to exchange data with that program, and what to do when something goes wrong. To work with existing applications, such as Web servers, you'll have to understand how the application-level protocols are built on top of TCP/IP, and how to deal with common data exchange formats such as

This book uses the Perl programming language to illustrate how to design and implement practical network applications. Perl is an ideal language for network programming for a number of reasons. First, like the rest of the language, Perl's networking facilities were designed to make the easy things easy. It takes just two lines of code to open a network connection to a server somewhere on the Internet and send it a message. A fully capable Web server can be written in a few dozen lines of code.

Second, Perl's open architecture has encouraged many talented programmers to contribute to an ever-expanding library of useful third-party modules. Many of these modules provide powerful interfaces to common network applications. For example, after loading the LWP::Simple module, a single function call allows you to fetch the contents of a remote Web page and store it in a variable. Other third-party modules provide intuitive interfaces to e-mail, FTP, net news, and a variety of network databases.

Perl also provides impressive portability. Most of the applications developed in this book will run without modification on UNIX machines, Windows boxes, Macintoshes, VMS systems, and OS/2.

However, the most compelling reason to choose Perl for network application development is that it allows you to fully exploit the power of TCP/IP. Perl provides you with full access to the same low-level networking calls that are available to C programs and other natively compiled languages. You can create multicast applications, implement multiplexed servers, and design peer-to-peer systems. Using Perl, you can rapidly prototype new networking applications and develop interfaces to existing ones. Should you ever need to write a networking application in C or Java, you'll be delighted to discover how much of the Perl API carries over into these languages.

This Book's Audience Network Programming with Perl is written for novice and intermediate Perl programmers. I assume you know the basics of Perl programming, including how to write loops, how to construct if-else statements, how to write regular expression pattern matches, the concept of the automatic $_ variable, and the basics of arrays and hashes.

You should have access to a Perl interpreter and some experience writing, running, and debugging scripts. Just as important, you should have access to a computer that is connected both to a local area network and to the Internet! Although the recipes in Chapter 10 on setting Perl-based network servers to start automatically when a machine is booted do require superuser (administrative) access, none of the other examples require privileged access to a machine.

This book does take advantage of the object-oriented features in Perl version 5 and higher, but most chapters do not assume a deep knowledge of this system. Chapter 1 addresses all the details you will need as a casual user of Perl objects.

This book is a thorough review of the TCP/IP protocol at the lowest level, or a guide to installing and configuring network hubs, routers, and name servers. Many good books on the mechanics of the TCP/IP protocol and network administration are listed in the references in Appendix D.

Roadmap

This book is organized into four main parts, Basics, Developing Cients for Common Services, Developing TCP Client/Server Systems, and Advanced Topics.

Part I, Basics, introduces the fundamentals of TCP/IP network communications.


  • Chapters 1 and 2, Networking Basics and Processes, Pipes, and Signals, review Perl's functions and variables for input and output, discuss the exceptions that can occur during I/O operations, and use the piped filehandle as the basis for introducing sockets. These chapters also review Perl's process model, including signals and forking, and introduce Perl's object-oriented extensions.
  • Chapter 3, Introduction to Berkeley Sockets, discusses the basics of Internet networking and describes IP addresses, network ports, and the principles of client/server applications. It then turns to the Berkeley Socket API, which provides the programmer's interface to TCP/IP.
  • Chapters 4 and 5, The TCP Protocol and The IO::Socket API and Simple TCP Applications, show the basics of TCP, the networking protocol that provides reliable stream-oriented communications. These chapters demonstrate how to create client and server applications and then introduce examples that show the power of technique as well as some common roadblocks.

Part II, Developing Clients for Common Services, looks at a collection of the best third-party modules that developers have contributed to the Comprehensive Perl Archive Network (CPAN).


  • Chapter 6, FTP and Telnet, introduces modules that provide access to the FTP file-sharing service, as well as to the flexible Net::Telnet module which allows you to create clients to access all sorts of network services.
  • E-mail is still the dominant application on the Internet, and Chapter 7, SMTP: Sending Mail, introduces half of the equation. This chapter shows you how to create e-mail messages on the fly, including binary attachments, and send them to their destinations.
  • Chapter 8, POP, IMAP, and NNTP: Processing Mail and Netnews, covers the other half of e-mail, explaining modules that make it possible to receive mail from mail drop systems and process their contents, including binary attachments.
  • Chapter 9, Web Clients, discusses the LWP module, which provides everything you need to talk to Web servers, download and process HTML documents, and parse

Part III, Developing TCP Client/Server Systems—the longest part of the book—discusses the alternatives for designing TCP-based client/server systems. The major example used in these chapters is an interactive psychotherapist server, based on Joseph Weizenbaum's classic Eliza program.


  • Chapter 10, Forking Servers and the inetd Daemon, covers the common type of TCP server that forks a new process to handle each incoming connection. This chapter also covers the UNIX and Windows inetd daemons, which allow programs not specifically designed for networking to act as servers.
  • Chapter 11, Multithreaded Applications, explains Perl's experimental multithreaded API, and shows how it can greatly simplify the design of TCP clients and servers.
  • Chapters 12 and 13, Multiplexed Operations and Nonblocking I/O, discuss the select() call, which enables an application to process multiple I/O streams concurrently without using multiprocessing or multithreading.
  • Chapter 14, Bulletproofing Servers, discusses techniques for enhancing the reliability and maintainability of network servers. Among the topics are logging, signal handling, and exceptions, as well as the important topic of network security.
  • Chapter 15, Preforking and Prethreading, presents the forking and threading models discussed in earlier chapters. These enhancements increase a server's ability to perform well under heavy loads.
  • Chapter 16, IO::Poll, discusses an alternative to select() available on UNIX platforms. This module allows applications to multiplex multiple I/O streams using an API that some people find more natural than select()'s.

Part IV, Advanced Topics, addresses techniques that are useful for specialized applications.


  • Chapter 17, TCP Urgent Data, is devoted to TCP urgent or "out of band" data. This technique is often used in highly interactive applications in which the user urgently needs to signal the remote server.
  • Chapters 18 and 19, The UDP Protocol and UDP Servers, introduce the User Datagram Protocol, which provides lightweight, message-oriented communications service. Chapter 18 introduces the protocol, and Chapter 19 shows how to design UDP servers. The major example in this and the next two chapters contain a live online chat and messaging system written entirely in Perl.
  • Chapters 20 and 21, Broadcasting and Multicasting, extend the UDP discussion by showing how to build one-to-all and one-to-many message broadcasting systems. In these chapters we extend the chat system to take advantage of automatic server discovery and multicasting.
  • Chapter 22, UNIX-Domain Sockets, shows how to create lightweight communications channels between processes on the same machine. This can be useful for specialized applications such as loggers.
    The Many Versions of Perl

All good things evolve to meet changing conditions, and Perl has gone through several major changes in the course of its short life. This book was written for versions of Perl in the 5.X series (5.003 and higher recommended). At the time I wrote this preface (August 2000), the most recent version of Perl was 5.6, with the release of 5.7 expected imminently. I expect that Perl versions 5.8 and 5.9 (assuming there will be such versions) will be compatible with the code examples given here as well.

Over the horizon, however, is Perl version 6. Version 6, which is expected to be in early alpha form by the summer of 2001, will fix many of the idiosyncrasies and misfeatures of earlier versions of Perl. In so doing, however, it is expected to break most existing scripts. Fortunately, the Perl language developers are committed to developing tools to automatically port existing scripts to version 6. With an eye to this, I have tried to make the examples in this book generic, avoiding the more obscure Perl constructions.

Cross-Platform Compatibility

More serious are the differences between implementations of Perl on various operating systems. Perl started out on UNIX (and Linux) systems, but has been ported to many different operating systems, including Microsoft Windows, the Macintosh, VMS, OS/2, Plan9, and others. A script written for the Windows platform will run on UNIX or Macintosh without modifications.

The problem is that the I/O subsystem (the part of the system that manages input and output operations) is the part that differs most dramatically from operating system to operating system. This restricts the ability of Perl to make its I/O system completely portable. While Perl's basic I/O functionality is identical from port to port, some of the more sophisticated operations are either missing or behave significantly differently on non-UNIX platforms. This affects network programming, of course, because networking is fundamentally about input and output.

In this book, Chapters 1 through 9, use generic networking calls that will run on all platforms. The exception to this rule is the last example in Chapter 5, which calls a function that isn't implemented on the Macintosh, fork(), and some of the introductory discussion in Chapter 2 of process management on UNIX systems. The techniques discussed in these chapters are all you need for the vast majority of client programs, and are sufficient to get a simple server up and running. Chapters 10 through 22 deal with more advanced topics in server design.

The nice thing is that the non-UNIX ports of Perl are improving rapidly, and there is a good chance that new features will be available at the time you read this.

Getting the Code for the Code Examples

All the sample scripts and modules discussed in this book are available on the Web in ZIP and TAR/GZIP formats. The URL for downloading the source is http://www.modperl.com/perl_networking. This page also includes instructions for unpacking and installing the source code.

Installing Modules

Many of Perl's networking modules are preinstalled in the standard distribution. Others are third-party modules that you must download and install from the Web. Most third-party modules are written in pure Perl, but some, including several that are mentioned in this book, are written partly in C and must be compiled before they can be used.

CPAN is a large Web-based collection of contributed Perl modules. You can get access to it via a Web or FTP browser, or by using a command-line application built into Perl itself.

Installing from the Web

To find a CPAN site near you, point your Web browser at http://www.cpan.org/. This will present a page that allows you to search for specific modules, or to browse the entire list of contributed modules sorted in various ways. When you find the module you want, download it to disk.

Perl modules are distributed as gzipped tar archives. You can unpack them like this:

% gunzip -c Digest-MD5-2.00.tar.gz tar xvf -
Digest-MD5-2.00/
Digest-MD5-2.00/typemap Digest-MD5-2.00/MD2/
Digest-MD5-2.00/MD2/MD2.pm
...

Once the archives are unpacked, you'll enter the newly created directory and give the perl Makefile.PL, make, make test, and make install commands. These will build, test, and install the module.

% cd Digest-MD5-2.00
% perl Makefile.PL Testing alignment requirements for U32...
Checking if your kit is complete...
Looks good Writing Makefile for Digest::MD2
Writing Makefile for Digest::MD5
% make mkdir ./blib mkdir ./blib/lib mkdir ./blib/lib/Digest
...
% make test make1: Entering directory '/home/lstein/Digest-MD5-2.00/MD2'
make1: Leaving directory '/home/lstein/Digest-MD5-2.00/MD2'
PERL_DL_NONLAZY=1 /usr/local/bin/perl -I./blib/arch -I./blib/lib...
t/digest............ok t/files.............ok t/md5-aaa...........ok t/md5...............ok t/rfc2202...........ok t/sha1..............skipping test on this platform All tests successful.
Files=6, Tests=291, 1 secs ( 1.37 cusr 0.08 csys = 1.45 cpu)
% make install make1: Entering directory '/home/lstein/Digest-MD5-2.00/MD2'
make1: Leaving directory '/home/lstein/Digest-MD5-2.00/MD2'
Installing /usr/local/lib/perl5/site_perl/i586-linux/./auto/Digest/MD5/MD5.so Installing /usr/local/lib/perl5/site_perl/i586-linux/./auto/Digest/MD5/MD5.bs
...

On UNIX systems, you may need superuser privileges to perform the final step. If you don't have such privileges, you can install the modules in your home directory. At the perl Makefile.PL step, provide a PREFIX= argument with the path of your home directory. For example, assuming your home directory can be found at /home/jdoe, you would type:

% perl Makefile.PL PREFIX=/home/jdoe

The rest of the install procedure is identical to what was shown earlier.

If you are using a custom install directory, you must tell Perl to look in this directory for installed modules. One way to do this is to add the name of the directory to the environment variable PERL5LIB. For example:

setenv PERL5LIB /home/jdoe # C shell PERL5LIB=/home/jdoe; export PERL5LIB # bourne shell

Another way is to place the following line at the top of each script that uses an installed module.

use lib '/home/jdoe'; Installing from the Command Line

A simpler way to do the same thing is to use Andreas Koenig's wonderful CPAN shell. With it, you can search, download, build, and install Perl modules from a simple command-line shell. The install command does it all:

% perl -MCPAN -e shell cpan shell — CPAN exploration and modules installation (v1.40)
ReadLine support enabled

cpan> install MD5
Running make for GAAS/Digest-MD5-2.00.tar.gz Fetching with LWP: ftp://ftp.cis.ufl.edu/pub/perl/CPAN/authors/id/GAAS/Digest-MD5-2.00.tar.gz CPAN: MD5 loaded ok Fetching with LWP: ftp://ftp.cis.ufl.edu/pub/perl/CPAN/authors/id/GAAS/CHECKSUMS
...
Checksum for /home/lstein/.cpan/sources/authors/id/GAAS/Digest-MD5-2.00.tar.gz ok Digest-MD5-2.00/
Digest-MD5-2.00/typemap
...
Installing /usr/local/lib/perl5/site_perl/i586-linux/./auto/Digest/MD5/MD5.so Installing /usr/local/lib/perl5/site_perl/i586-linux/./auto/Digest/MD5/MD5.bs Installing /usr/local/lib/perl5/site_perl/i586-linux/./auto/MD5/MD5.so
...
Writing /usr/local/lib/perl5/site_perl/i586-linux/auto/MD5/.packlist Appending installation info to /usr/local/lib/perl5/i586-linux/5.00404/perllocal.pod

cpan> exit Installing Modules with the Perl Package Manager

These examples all assume that you have UNIX-compatible versions of the gzip, tar, and make commands. Virgin Windows systems do not have these utilities. The Cygwin package, available from http://www.cygnus.com/cygwin/, provides these utilities as part of a complete set of UNIX-compatible tools.

It is easier, however, to use the ActiveState Perl Package Manager (PPM). This Perl script is installed by default in the ActiveState distribution of Perl, available at http://www.activestate.com. Its interface is similar to the command-line CPAN interface shown in the previous section, except that it can install precompiled binaries as well as pure-Perl scripts. For example:

C:\WINDOWS>ppm
PPM interactive shell (1.1.3) - type 'help' for available commands.
PPM> install MD5
Install package 'MD5?' (y/N) : Y
Retrieving package 'MD5'
Installing C:\Perl\site\lib\auto\MD5\MD5.bs Installing C:\Perl\site\lib\auto\MD5\MD5.dll Installing C:\Perl\site\lib\auto\MD5\MD5.exp Installing C:\Perl\site\lib\auto\MD5\MD5.lib Installing C:\Perl\site\lib\MD5.pm Installing C:\Perl\site\lib\auto\MD5\autosplit.ix Writing C:\Perl\sitelib\auto\MD5\.packlist PPM> exit
Quit!
C:\WINDOWS>
Installing Modules from MacPerl

The MacPerl Module Porters site, http://pudge.net/cgi-bin/mmp.plx, contains a series of modules that have been ported for use in MacPerl. A variety of helper programs have been developed to make module installation easier on the Macintosh. The packages are described at http://pudge.net/macperl/macperlmodinstall.html, which also gives instructions on downloading and installing them.

Online Documentation

In addition to books and Web sites, Network Programming with Perl refers to two major sources of online information, Internet RFCs and Perl POD documentation.

Internet RFCs

The specifications of all the fundamental protocols of the Internet are described in a series of Requests for Comment (RFC) submitted to the Internet Engineering Task Force (IETF). These documents are numbered sequentially. For example RFC 1927—"Suggested Additional MIME Types for Associating Documents"—was the 1927th RFC submitted. Some of these RFCs eventually become Internet Standards, in which case they are given sequentially numbered STD names. However, most of them remain RFCs. Even though the RFCs are unofficial, they are the references that people use to learn the details of networking protocols and to validate that a particular implementation is correct.

The RFC archives are mirrored at many locations on the Internet, and maintained in searchable form by several organizations. One of the best archives is maintained at http://www.faqs.org/rfcs/. To retrieve an RFC from this site, go to the indicated page and type the number of the desired RFC in the text field labeled "Display the document by number." The document will be delivered in a minimally HTMLized form. This page also allows you to search for standards documents, and to search the archive by keywords and phrases. If you prefer a text-only form, the www.faqs.org site contains a link to their FTP site, where you can find and download the RFCs in their original form.

Plain Old Documentation

Much of Perl's internal documentation comes in Plain Old Documentation (POD) format. These are mostly plain text, with a few markup elements inserted to indicate headings, subheadings, and itemized lists.

When you installed Perl, the POD documentation was installed as well. The POD files are located in the pod subdirectory of the Perl library directory. You can either read them directly, or use the perldoc script to format and display them in a text pager such as more.

To use perldoc type the command and the name of the POD file you wish to view. The best place to start is the Perl table of contents, perltoc:

% perldoc perltoc

This will give you a list of other POD pages that you can display.

For a quick summary of a particular Perl function, perldoc accepts the -f flag. For example, to see a summary of the socket() function, type:

% perldoc -f socket

For Macintosh user's the MacPerl distribution comes with a "helper" application called shuck. This adds POD viewing facilities to the MacPerl Help menu.

Read More Show Less

Table of Contents

Preface.

I. BASICS.

1. Input/Output Basics.

Perl and Networking.

Networking Made Easy.

Filehandles.

Using Object-Oriented Syntax with the IO::Handle and IO::File Modules.

Summary.

2. Processes, Pipes, and Signals.

Processes.

Pipes.

Signals.

Summary.

3. Introduction to Berkeley Sockets.

Clients, Servers, and Protocols.

Berkeley Sockets.

Socket Addressing.

A Simple Network Client.

Network Names and Services.

Network Analysis Tools.

Summary.

4. The TCP Protocol.

A TCP Echo Client.

Socket Functions Related to Outgoing Connections.

A TCP Echo Server.

Adjusting Socket Options.

Other Socket-Related Functions.

Exceptional Conditions during TCP Communications.

Summary.

5. The IO::Socket API.

Using IO::Socket.

IO::Socket Methods.

More Practical Examples.

Performance and Style.

Concurrent Clients.

Summary.

II. DEVELOPING CLIENTS FOR COMMON SERVICES.

6. FTP and Telnet.

Net::FTP.

Net::Telnet.

Summary.

7. SMTP: Sending Mail.

Introduction to the Mail Modules.

Net::SMTP.

MailTools.

MIME-Tools.

Summary.

8. POP, IMAP, and NNTP: Processing Mail and Netnews.

The Post Office Protocol.

The IMAP Protocol.

Internet News Clients.

A News-to-Mail Gateway.

Summary.

9. Web Clients.

Installing LWP.

LWP Basics.

LWP Examples.

Parsing HTML and XML.

Summary.

III. DEVELOPING TCP CLIENT/SERVER SYSTEMS.

10. Forking Servers and the inetd Daemon.

Standard Techniques for Concurrency.

Running Example: A Psychotherapist Server.

The Psychotherapist as a Forking Server.

A Client Script for the Psychotherapist Server.

Daemonization on UNIX Systems.

Starting Network Servers Automatically.

Using the inetd Super Daemon.

Summary.

11. Multithreaded Applications.

About Threads.

A Multithreaded Psychiatrist Server.

A Multithreaded Client.

Summary.

12. Multiplexed Applications.

A Multiplexed Client.

The IO::Select Module.

A Multiplexed Psychiatrist Server.

Summary.

13. Nonblocking I/O.

Creating Nonblocking I/O Handles.

Using Nonblocking Handles.

Using Nonblocking Handles with Line-Oriented I/O.

A Generic Nonblocking I/O Module.

Nonblocking Connects and Accepts.

Summary.

14. Bulletproofing Servers.

Using the System Log.

Setting User Privileges.

Taint Mode.

Using chroot().

Handling HUP and Other Signals.

Summary.

15. Preforking and Prethreading.

Preforking.

Prethreading.

Performance Measures.

Summary.

16. IO::Poll.

Using IO::Poll.

IO::Poll Methods.

A Nonblocking TCP Client Using IO::Poll.

Summary.

IV. ADVANCED TOPICS.

17. TCP Urgent Data.

"Out-of-Band" Data and the Urgent Pointer.

Using TCP Urgent Data.

The sockatmark() Function.

A Travesty Server.

Summary.

18. The UDP Protocol.

A Time of Day Client.

Creating and Using UDP Sockets.

UDP Errors.

Using UDP Sockets with IO::Socket.

Sending to Multiple Hosts.

UDP Servers.

Increasing the Robustness of UDP Applications.

Summary.

19. UDP Servers.

An Internet Chat System.

The Chat Client.

The Chat Server.

Detecting Dead Clients.

Summary.

20. Broadcasting.

Unicasting versus Broadcasting.

Broadcasting Explained.

Sending and Receiving Broadcasts.

Broadcasting Without the Broadcast Address.

Enhancing the Chat Client to Support Resource Discovery.

Summary.

21. Multicasting.

Multicast Basics.

Using Multicast.

Sample Multicast Applications.

Summary.

22. UNIX-Domain Sockets.

Using UNIX-Domain Sockets.

A "Wrap" Server.

Using UNIX-Domain Sockets for Datagrams.

Summary.

Appendix A. Additional Source Code.

Net::NetmaskLite (Chapter 3).

PromptUtil.pm (Chapters 8 and 9).

IO::LineBufferedSet (Chapter 13).

IO::LineBufferedSessionData (Chapter 13).

DaemonDebug (Chapter 14).

Text::Travesty (Chapter 17).

mchat_client.pl (Chapter 21).

Appendix B. Perl Error Codes and Special Variables.

System Error Constants.

Magic Variables Affecting I/O.

Other Perl Globals.

Appendix C. Internet Reference Tables.

Assigned Port Numbers.

Registered Port Numbers.

Internet Multicast Addresses.

Appendix D. Bibliography.

Index. 0201615711T04062001

Read More Show Less

Preface

The network is everywhere. At the office, machines are wired together into local area networks, and the local networks are interconnected via the Internet. At home, personal computers are either intermittently connected to the Internet, or, increasingly, "always-on" cable and DSL modems. New wireless technologies, such as Bluetooth, promise to vastly expand the network realm, embracing everything from cell phones to kitchen appliances.

Such an environment creates tremendous opportunities for innovation. Whole new classes of applications are now predicated on the availability of high-bandwidth, always-on connectivity. Interactive games allow players from around the globe to compete on virtual playing fields and the instant messaging protocols let them broadcast news of their triumphs to their friends. New peer-to-peer systems, such as Napster and Gnutella, allow people to directly exchange MP3 audio files and other types of digital content. The SETI@Home project takes advantage of idle time on the millions of personal computers around the world to search for signs of extraterrestrial life in a vast collection of cosmic noise.

The ubiquity of the network allows for more earthbound applications as well. With the right knowledge, you can write a robot that will fetch and summarize prices from competitors' Web sites; a script to page you when a certain stock drops below a specified level; a program to generate daily management reports and send them off via e-mail; a server that centralizes some number-crunching task on a single high-powered machine, or alternatively distributes that task among the multiple nodes of a computer cluster.

Whether you are searching for the best price on a futon or for life in a distant galaxy, you'll need to understand how network applications work in order to take full advantage of these opportunities. You'll need a working understanding of the TCP/IP protocol--the common denominator for all Internet-based communications and the most common protocol in use in local area networks as well. You'll need to know how to connect to a remote program, to exchange data with that program, and what to do when something goes wrong. To work with existing applications, such as Web servers, you'll have to understand how the application-level protocols are built on top of TCP/IP, and how to deal with common data exchange formats such as XML and MIME.

This book uses the Perl programming language to illustrate how to design and implement practical network applications. Perl is an ideal language for network programming for a number of reasons. First, like the rest of the language, Perl's networking facilities were designed to make the easy things easy. It takes just two lines of code to open a network connection to a server somewhere on the Internet and send it a message. A fully capable Web server can be written in a few dozen lines of code.

Second, Perl's open architecture has encouraged many talented programmers to contribute to an ever-expanding library of useful third-party modules. Many of these modules provide powerful interfaces to common network applications. For example, after loading the LWP::Simple module, a single function call allows you to fetch the contents of a remote Web page and store it in a variable. Other third-party modules provide intuitive interfaces to e-mail, FTP, net news, and a variety of network databases.

Perl also provides impressive portability. Most of the applications developed in this book will run without modification on UNIX machines, Windows boxes, Macintoshes, VMS systems, and OS/2.

However, the most compelling reason to choose Perl for network application development is that it allows you to fully exploit the power of TCP/IP. Perl provides you with full access to the same low-level networking calls that are available to C programs and other natively compiled languages. You can create multicast applications, implement multiplexed servers, and design peer-to-peer systems. Using Perl, you can rapidly prototype new networking applications and develop interfaces to existing ones. Should you ever need to write a networking application in C or Java, you'll be delighted to discover how much of the Perl API carries over into these languages.

This Book's Audience

Network Programming with Perl is written for novice and intermediate Perl programmers. I assume you know the basics of Perl programming, including how to write loops, how to construct if-else statements, how to write regular expression pattern matches, the concept of the automatic $_ variable, and the basics of arrays and hashes.

You should have access to a Perl interpreter and some experience writing, running, and debugging scripts. Just as important, you should have access to a computer that is connected both to a local area network and to the Internet! Although the recipes in Chapter 10 on setting Perl-based network servers to start automatically when a machine is booted do require superuser (administrative) access, none of the other examples require privileged access to a machine.

This book does take advantage of the object-oriented features in Perl version 5 and higher, but most chapters do not assume a deep knowledge of this system. Chapter 1 addresses all the details you will need as a casual user of Perl objects.

This book is a thorough review of the TCP/IP protocol at the lowest level, or a guide to installing and configuring network hubs, routers, and name servers. Many good books on the mechanics of the TCP/IP protocol and network administration are listed in the references in Appendix D.

Roadmap

This book is organized into four main parts, Basics, Developing Cients for Common Services, Developing TCP Client/Server Systems, and Advanced Topics.

Part I, Basics, introduces the fundamentals of TCP/IP network communications.

  • Chapters 1 and 2, Networking Basics and Processes, Pipes, and Signals, review Perl's functions and variables for input and output, discuss the exceptions that can occur during I/O operations, and use the piped filehandle as the basis for introducing sockets. These chapters also review Perl's process model, including signals and forking, and introduce Perl's object-oriented extensions.
  • Chapter 3, Introduction to Berkeley Sockets, discusses the basics of Internet networking and describes IP addresses, network ports, and the principles of client/server applications. It then turns to the Berkeley Socket API, which provides the programmer's interface to TCP/IP.
  • Chapters 4 and 5, The TCP Protocol and The IO::Socket API and Simple TCP Applications, show the basics of TCP, the networking protocol that provides reliable stream-oriented communications. These chapters demonstrate how to create client and server applications and then introduce examples that show the power of technique as well as some common roadblocks.

Part II, Developing Clients for Common Services, looks at a collection of the best third-party modules that developers have contributed to the Comprehensive Perl Archive Network (CPAN).

  • Chapter 6, FTP and Telnet, introduces modules that provide access to the FTP file-sharing service, as well as to the flexible Net::Telnet module which allows you to create clients to access all sorts of network services.
  • E-mail is still the dominant application on the Internet, and Chapter 7, SMTP: Sending Mail, introduces half of the equation. This chapter shows you how to create e-mail messages on the fly, including binary attachments, and send them to their destinations.
  • Chapter 8, POP, IMAP, and NNTP: Processing Mail and Netnews, covers the other half of e-mail, explaining modules that make it possible to receive mail from mail drop systems and process their contents, including binary attachments.
  • Chapter 9, Web Clients, discusses the LWP module, which provides everything you need to talk to Web servers, download and process HTML documents, and parse XML.

Part III, Developing TCP Client/Server Systems--the longest part of the book--discusses the alternatives for designing TCP-based client/server systems. The major example used in these chapters is an interactive psychotherapist server, based on Joseph Weizenbaum's classic Eliza program.

  • Chapter 10, Forking Servers and the inetd Daemon, covers the common type of TCP server that forks a new process to handle each incoming connection. This chapter also covers the UNIX and Windows inetd daemons, which allow programs not specifically designed for networking to act as servers.
  • Chapter 11, Multithreaded Applications, explains Perl's experimental multithreaded API, and shows how it can greatly simplify the design of TCP clients and servers.
  • Chapters 12 and 13, Multiplexed Operations and Nonblocking I/O, discuss the select() call, which enables an application to process multiple I/O streams concurrently without using multiprocessing or multithreading.
  • Chapter 14, Bulletproofing Servers, discusses techniques for enhancing the reliability and maintainability of network servers. Among the topics are logging, signal handling, and exceptions, as well as the important topic of network security.
  • Chapter 15, Preforking and Prethreading, presents the forking and threading models discussed in earlier chapters. These enhancements increase a server's ability to perform well under heavy loads.
  • Chapter 16, IO::Poll, discusses an alternative to select() available on UNIX platforms. This module allows applications to multiplex multiple I/O streams using an API that some people find more natural than select()'s.

Part IV, Advanced Topics, addresses techniques that are useful for specialized applications.

  • Chapter 17, TCP Urgent Data, is devoted to TCP urgent or "out of band" data. This technique is often used in highly interactive applications in which the user urgently needs to signal the remote server.
  • Chapters 18 and 19, The UDP Protocol and UDP Servers, introduce the User Datagram Protocol, which provides lightweight, message-oriented communications service. Chapter 18 introduces the protocol, and Chapter 19 shows how to design UDP servers. The major example in this and the next two chapters contain a live online chat and messaging system written entirely in Perl.
  • Chapters 20 and 21, Broadcasting and Multicasting, extend the UDP discussion by showing how to build one-to-all and one-to-many message broadcasting systems. In these chapters we extend the chat system to take advantage of automatic server discovery and multicasting.
  • Chapter 22, UNIX-Domain Sockets, shows how to create lightweight communications channels between processes on the same machine. This can be useful for specialized applications such as loggers.

The Many Versions of Perl

All good things evolve to meet changing conditions, and Perl has gone through several major changes in the course of its short life. This book was written for versions of Perl in the 5.X series (5.003 and higher recommended). At the time I wrote this preface (August 2000), the most recent version of Perl was 5.6, with the release of 5.7 expected imminently. I expect that Perl versions 5.8 and 5.9 (assuming there will be such versions) will be compatible with the code examples given here as well.

Over the horizon, however, is Perl version 6. Version 6, which is expected to be in early alpha form by the summer of 2001, will fix many of the idiosyncrasies and misfeatures of earlier versions of Perl. In so doing, however, it is expected to break most existing scripts. Fortunately, the Perl language developers are committed to developing tools to automatically port existing scripts to version 6. With an eye to this, I have tried to make the examples in this book generic, avoiding the more obscure Perl constructions.

Cross-Platform Compatibility

More serious are the differences between implementations of Perl on various operating systems. Perl started out on UNIX (and Linux) systems, but has been ported to many different operating systems, including Microsoft Windows, the Macintosh, VMS, OS/2, Plan9, and others. A script written for the Windows platform will run on UNIX or Macintosh without modifications.

The problem is that the I/O subsystem (the part of the system that manages input and output operations) is the part that differs most dramatically from operating system to operating system. This restricts the ability of Perl to make its I/O system completely portable. While Perl's basic I/O functionality is identical from port to port, some of the more sophisticated operations are either missing or behave significantly differently on non-UNIX platforms. This affects network programming, of course, because networking is fundamentally about input and output.

In this book, Chapters 1 through 9, use generic networking calls that will run on all platforms. The exception to this rule is the last example in Chapter 5, which calls a function that isn't implemented on the Macintosh, fork(), and some of the introductory discussion in Chapter 2 of process management on UNIX systems. The techniques discussed in these chapters are all you need for the vast majority of client programs, and are sufficient to get a simple server up and running. Chapters 10 through 22 deal with more advanced topics in server design.

The nice thing is that the non-UNIX ports of Perl are improving rapidly, and there is a good chance that new features will be available at the time you read this.

Getting the Code for the Code Examples

All the sample scripts and modules discussed in this book are available on the Web in ZIP and TAR/GZIP formats. The URL for downloading the source is http://www.modperl.com/perl_networking. This page also includes instructions for unpacking and installing the source code.

Installing Modules

Many of Perl's networking modules are preinstalled in the standard distribution. Others are third-party modules that you must download and install from the Web. Most third-party modules are written in pure Perl, but some, including several that are mentioned in this book, are written partly in C and must be compiled before they can be used.

CPAN is a large Web-based collection of contributed Perl modules. You can get access to it via a Web or FTP browser, or by using a command-line application built into Perl itself.

Installing from the Web

To find a CPAN site near you, point your Web browser at http://www.cpan.org/. This will present a page that allows you to search for specific modules, or to browse the entire list of contributed modules sorted in various ways. When you find the module you want, download it to disk.

Perl modules are distributed as gzipped tar archives. You can unpack them like this:

% gunzip -c Digest-MD5-2.00.tar.gz tar xvf -
Digest-MD5-2.00/
Digest-MD5-2.00/typemap
Digest-MD5-2.00/MD2/
Digest-MD5-2.00/MD2/MD2.pm
...

Once the archives are unpacked, you'll enter the newly created directory and give the perl Makefile.PL, make, make test, and make install commands. These will build, test, and install the module.

% cd Digest-MD5-2.00
% perl Makefile.PL
Testing alignment requirements for U32...
Checking if your kit is complete...
Looks good
Writing Makefile for Digest::MD2
Writing Makefile for Digest::MD5
% make
mkdir ./blib
mkdir ./blib/lib
mkdir ./blib/lib/Digest
...
% make test
make1: Entering directory '/home/lstein/Digest-MD5-2.00/MD2'
make1: Leaving directory '/home/lstein/Digest-MD5-2.00/MD2'
PERL_DL_NONLAZY=1 /usr/local/bin/perl -I./blib/arch -I./blib/lib...
t/digest............ok
t/files.............ok
t/md5-aaa...........ok
t/md5...............ok
t/rfc2202...........ok
t/sha1..............skipping test on this platform
All tests successful.
Files=6, Tests=291, 1 secs ( 1.37 cusr 0.08 csys = 1.45 cpu)
% make install
make1: Entering directory '/home/lstein/Digest-MD5-2.00/MD2'
make1: Leaving directory '/home/lstein/Digest-MD5-2.00/MD2'
Installing /usr/local/lib/perl5/site_perl/i586-linux/./auto/Digest/MD5/MD5.so
Installing /usr/local/lib/perl5/site_perl/i586-linux/./auto/Digest/MD5/MD5.bs
...

On UNIX systems, you may need superuser privileges to perform the final step. If you don't have such privileges, you can install the modules in your home directory. At the perl Makefile.PL step, provide a PREFIX= argument with the path of your home directory. For example, assuming your home directory can be found at /home/jdoe, you would type:

% perl Makefile.PL PREFIX=/home/jdoe

The rest of the install procedure is identical to what was shown earlier.

If you are using a custom install directory, you must tell Perl to look in this directory for installed modules. One way to do this is to add the name of the directory to the environment variable PERL5LIB. For example:

setenv PERL5LIB /home/jdoe # C shell
PERL5LIB=/home/jdoe; export PERL5LIB # bourne shell

Another way is to place the following line at the top of each script that uses an installed module.

use lib '/home/jdoe';

Installing from the Command Line

A simpler way to do the same thing is to use Andreas Koenig's wonderful CPAN shell. With it, you can search, download, build, and install Perl modules from a simple command-line shell. The install command does it all:

% perl -MCPAN -e shell cpan shell -- CPAN exploration and modules installation (v1.40)
ReadLine support enabled

cpan> install MD5
Running make for GAAS/Digest-MD5-2.00.tar.gz
Fetching with LWP: ftp://ftp.cis.ufl.edu/pub/perl/CPAN/authors/id/GAAS/Digest-MD5-2.00.tar.gz
CPAN: MD5 loaded ok
Fetching with LWP: ftp://ftp.cis.ufl.edu/pub/perl/CPAN/authors/id/GAAS/CHECKSUMS
...
Checksum for /home/lstein/.cpan/sources/authors/id/GAAS/Digest-MD5-2.00.tar.gz
ok Digest-MD5-2.00/
Digest-MD5-2.00/typemap
...
Installing /usr/local/lib/perl5/site_perl/i586-linux/./auto/Digest/MD5/MD5.so
Installing /usr/local/lib/perl5/site_perl/i586-linux/./auto/Digest/MD5/MD5.bs
Installing /usr/local/lib/perl5/site_perl/i586-linux/./auto/MD5/MD5.so
...
Writing /usr/local/lib/perl5/site_perl/i586-linux/auto/MD5/.packlist
Appending installation info to /usr/local/lib/perl5/i586-linux/5.00404/perllocal.pod

cpan> exit

Installing Modules with the Perl Package Manager

These examples all assume that you have UNIX-compatible versions of the gzip, tar, and make commands. Virgin Windows systems do not have these utilities. The Cygwin package, available from http://www.cygnus.com/cygwin/, provides these utilities as part of a complete set of UNIX-compatible tools.

It is easier, however, to use the ActiveState Perl Package Manager (PPM). This Perl script is installed by default in the ActiveState distribution of Perl, available at http://www.activestate.com. Its interface is similar to the command-line CPAN interface shown in the previous section, except that it can install precompiled binaries as well as pure-Perl scripts. For example:

C:\WINDOWS> ppm
PPM interactive shell (1.1.3) - type 'help' for available commands.
PPM> install MD5
Install package 'MD5?' (y/N) : Y
Retrieving package 'MD5'
Installing C:\Perl\site\lib\auto\MD5\MD5.bs
Installing C:\Perl\site\lib\auto\MD5\MD5.dll
Installing C:\Perl\site\lib\auto\MD5\MD5.exp
Installing C:\Perl\site\lib\auto\MD5\MD5.lib
Installing C:\Perl\site\lib\MD5.pm
Installing C:\Perl\site\lib\auto\MD5\autosplit.ix
Writing C:\Perl\sitelib\auto\MD5\.packlist
PPM> exit
Quit!
C:\WINDOWS>

Installing Modules from MacPerl

The MacPerl Module Porters site, http://pudge.net/cgi-bin/mmp.plx, contains a series of modules that have been ported for use in MacPerl. A variety of helper programs have been developed to make module installation easier on the Macintosh. The packages are described at http://pudge.net/macperl/macperlmodinstall.html, which also gives instructions on downloading and installing them.

Online Documentation

In addition to books and Web sites, Network Programming with Perl refers to two major sources of online information, Internet RFCs and Perl POD documentation.

Internet RFCs

The specifications of all the fundamental protocols of the Internet are described in a series of Requests for Comment (RFC) submitted to the Internet Engineering Task Force (IETF). These documents are numbered sequentially. For example RFC 1927--"Suggested Additional MIME Types for Associating Documents"--was the 1927th RFC submitted. Some of these RFCs eventually become Internet Standards, in which case they are given sequentially numbered STD names. However, most of them remain RFCs. Even though the RFCs are unofficial, they are the references that people use to learn the details of networking protocols and to validate that a particular implementation is correct.

The RFC archives are mirrored at many locations on the Internet, and maintained in searchable form by several organizations. One of the best archives is maintained at http://www.faqs.org/rfcs/. To retrieve an RFC from this site, go to the indicated page and type the number of the desired RFC in the text field labeled "Display the document by number." The document will be delivered in a minimally HTMLized form. This page also allows you to search for standards documents, and to search the archive by keywords and phrases. If you prefer a text-only form, the www.faqs.org site contains a link to their FTP site, where you can find and download the RFCs in their original form.

Plain Old Documentation

Much of Perl's internal documentation comes in Plain Old Documentation (POD) format. These are mostly plain text, with a few markup elements inserted to indicate headings, subheadings, and itemized lists.

When you installed Perl, the POD documentation was installed as well. The POD files are located in the pod subdirectory of the Perl library directory. You can either read them directly, or use the perldoc script to format and display them in a text pager such as more.

To use perldoc type the command and the name of the POD file you wish to view. The best place to start is the Perl table of contents, perltoc:

% perldoc perltoc

This will give you a list of other POD pages that you can display.

For a quick summary of a particular Perl function, perldoc accepts the -f flag. For example, to see a summary of the socket() function, type:

% perldoc -f socket

For Macintosh user's the MacPerl distribution comes with a "helper" application called shuck. This adds POD viewing facilities to the MacPerl Help menu.

0201615711P04062001

Read More Show Less

Customer Reviews

Average Rating 5
( 2 )
Rating Distribution

5 Star

(2)

4 Star

(0)

3 Star

(0)

2 Star

(0)

1 Star

(0)

Your Rating:

Your Name: Create a Pen Name or

Barnes & Noble.com Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & Noble.com that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & Noble.com does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at BN.com or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation

Reminder:

  • - By submitting a review, you grant to Barnes & Noble.com and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Noble.com Terms of Use.
  • - Barnes & Noble.com reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & Noble.com also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on BN.com. It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

 
Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously
Sort by: Showing all of 2 Customer Reviews
  • Anonymous

    Posted December 26, 2001

    Incredibly useful and very clearly written

    First off, this book assumes an intermediate knowledge of perl. With that out of the way, this is the best programming book that I own. It is perfectly organized and the explanation/documentation are crystal clear- written line by line. Each chapter starts off with a very basic program and goes to an advanced program at the end of every chapter. If you are a UNIX sys admin or an aspiring one BUY THIS BOOK!

    Was this review helpful? Yes  No   Report this review
  • Anonymous

    Posted March 4, 2001

    The one you have been waiting for...

    This title has taken its place on my shelf next to the Camel and Object Oriented Perl ... This is not a terse, vague coverage, but a thorough, detailed instruction on many aspects of network programming. Perl documentation provides the syntax of using sockets, but Stein shows how to actually use them ... and seems to gloss over no sublety... All major points illustrated *effectively* with source code and working examples ... As an experienced Perl programmer, I consider this book indispensable ... one of the most useful books I own.

    Was this review helpful? Yes  No   Report this review
Sort by: Showing all of 2 Customer Reviews

If you find inappropriate content, please report it to Barnes & Noble
Why is this product inappropriate?
Comments (optional)