Network programming is hard. The C socket library is the standard way of writing Internet clients and servers. It’s like the file API described in Chapter 7, with its special flags and meager abstraction, only much more complicated. It’s a shame because networked applications are the coolest kind of application. Only computer nerds like us care about XML or the best way to sort a list, but everyone uses Internet applications.
Fortunately, network programming is easy. Ruby provides bindings to the C socket library (in
socket), but you’ll probably never need to use them. Existing Ruby libraries (some in the standard distribution) can speak every popular high-level Internet protocol.
The most popular Internet service is, of course, the Web, and Ruby’s most popular Internet library (or any kind of library, actually) is the Rails framework. We’ve devoted the entire next chapter to Rails (Chapter 16) so that we can cover other technologies here.
Apart from Rails, most of the interesting stuff you can do with Ruby happens on the client end. We start with a set of recipes for requesting web pages (Recipes 15.1, 15.2, and 15.3), which are brought together at the end of the chapter with Recipe 15.20. Combine these recipes with one from Chapter 12, and you can make your own spider or web browser.
Then we present Ruby clients for the most popular Internet protocols. Ruby can do just about everything you do online: send and receive email, perform nameserver queries, ...