Chapter9. Network and Web Programming

 
   

Ruby Way
By Hal Fulton
Slots : 1.0
Table of Contents
 


Chapter 9. Network and Web Programming

IN THIS CHAPTER

  • Network Servers

  • Network Clients

  • Ruby and the Web Server

  • Ruby and CGI Programming

  • Distributed Ruby

  • XML Parsing in Ruby

  • Summary

Never underestimate the bandwidth of a station wagon full of tapes hurtling down the highway.

Andrew S. Tanenbaum

This book strives for compactness. As such, we've put networking and Web development in the same chapter (and even squeezed in some XML examples). In a broad sense, these topics belong together. Without the lower-level networking in place, it would be impossible for a browser to communicate with a Web server sending and receiving information. Of course, separate chapters could be devoted to these areas; in fact, separate books could be devoted to them. But we've decided to do what we could in a limited space.

When a marketing type says networking, he probably means that he wants to give you his business card. But when a programmer says it, he's talking about electronic communication between physically separated machineswhether across the room, across the city, or across the world.

In the authors' world, networking usually implies TCP/IP, the native tongue in which millions of machines whisper back and forth across the Internet. We'll say a few words about this before diving into some concrete examples.

Network communication is conceptualized at different levels (or layers) of abstraction. The lowest level is the data link layer, or actual hardware-level communication, which we need not discuss here. Immediately above this is the network layer, which is concerned with the actual moving around of packets; this is the realm of IP (Internet Protocol). At a still higher level of abstraction is the transport layer, where we find TCP (Transmission Control Protocol) and UDP (User Datagram Protocol). At the level above this, we find the application layer; at this point we finally enter the world of telnet, FTP, e-mail protocols, and much more.

It's possible to communicate directly in IP, but normally you wouldn't do such a thing. Most of the time, we are concerned with TCP or UDP.

TCP provides reliable communication between two hosts; it is concerned with the blocking and deblocking of packet data, acknowledgement of receipt, handling timeouts, reassembling the packets in the proper order, and so on. Because it is a reliable protocol, the application using it need not worry about a packet in the middle of a message not arriving; packets are re-sent as needed.

UDP is much simpler, merely sending packets (datagrams) to the remote host, like binary postcards. There is no guarantee that these will be received, nor that the ones received will be in order, so the protocol is unreliable (and thus the application has some extra details to worry about.) It's logical to use UDP when you have a simple, short message that will fit in a single packet, and it is convenient to handle a timeout manually. In such a case, TCP might be overkill.

Ruby supports low-level networking (chiefly in TCP and UDP), as well as coding at higher levels. These higher levels include applications for remote login, file transfer, e-mail, and others (for example, telnet, FTP, and SMTP).

Figure 9.1 is a class hierarchy showing the highlights of Ruby's networking support. We include HTTP and certain other high-level items here; some others are omitted for clarity.

Figure 9.1. Partial Inheritance hierarchy for networking support in Ruby.

graphics/09fig01.gif


Note that the bulk of these classes derive from the IO class. This means that we can use the methods of IO that are so familiar to us.

We'd like to document all the features of all these classes. Unfortunately, that task would exceed our space requirements. We only present a task-oriented approach to all this and offer a little explanation. For a comprehensive list of available methods, consult a reference.

A few significant areas are not covered here at all, so we'll mention these up front. The Net::Telnet class is covered only in connection with NTP servers in the section "Contacting an Official Timeserver" this class is not just for implementing your own telnet client, but is potentially useful for automating anything that has a telnet interface.

The Net::FTP library is also not covered here. In general, FTP is very easy to automate in its everyday form, so there is less motivation to use this class than there might be for some others.

The Net::Protocol class is the parent of HTTP, POP3, and SMTP. It would probably prove useful in the development of other customized networking protocols, but time and space prevent us from delving into something of that nature.

That ends our broad overview. Let's look at some code.


   

 

 



The Ruby Way
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2000
Pages: 119
Authors: Hal Fulton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net