Using IO::Socket


 
Network Programming with Perl
By Lincoln  D.  Stein
Slots : 1
Table of Contents
Chapter  5.   The IO::Socket API

    Content

Before we discuss IO::Socket in detail, let's get a feel for it by reimplementing some of the examples from Chapters 3 and 4.

A Daytime Client

The first example is a rewritten version of the daytime service client developed in Chapter 3 (Figure 3.7). As you recall, this client establishes an outgoing connection to a daytime server, reads a line, and exits.

This is a good opportunity to fix a minor bug in the original examples (left there in the interests of not unnecessarily complicating the code). Like many Internet servers, the daytime service terminates its lines with CRLF rather than a single LF as Perl does. Before reading from daytime, we set the end-of-line character to CRLF. Otherwise, the line we read will contain an extraneous CR at the end.

Figure 5.1 lists the code.

Figure 5.1. Time of day client using IO::Socket

graphics/05fig01.gif

Lines 1 “4: Initialize module We load IO::Socket and import the ":crlf" constants as well as the default constants. These constants are conveniently reexported from Socket. We recover the name of the remote host from the command line.

Line 5: Set the end-of-line separator To read lines from the daytime server, we set the $/ end-of-line global to CRLF . Note that this global option affects all filehandles, not just the socket.

Lines 6 “7: Create socket We create a new IO::Socket object by calling IO::Socket::INET's new method, specifying the destination address in the form $host:service. We will see other ways to specify the destination address later.

Lines 8 “9: Read the time of day and print it We read a single line from the server by calling getline() , and remove the CRLF from the end with chomp() . We print this line to STDOUT .

When we run this program, we get the expected result:

 % time_of_day_tcp.pl wuarchive.wustl.edu Tue Aug 15 07:39:49 2000 

TCP Echo Client

Now we'll look at an object-oriented version of the echo service client from Chapter 4. Figure 5.2 lists the code.

Figure 5.2. TCP echo client using IO::Socket

graphics/05fig02.gif

Lines 1 “8: Initialize script We load IO::Socket, initialize constants and globals , and process the command-line arguments.

Line 9: Create socket We call the IO::Socket::INET->new() method using the $host:$port argument. If new() is successful, it returns a socket object connected to the remote host.

Lines 10 “16: Enter main loop We now enter the main loop. Each time through the loop we call getline() on the STDIN filehandle to retrieve a line of input from the user . We send this line of text to the remote host using print() on the socket, and read the server's response using the <> operator. We print the response to standard output, and update the statistics.

Lines 17 “18: Clean up The main loop will end when STDIN is closed by the user. We close the socket and print the accumulated statistics to STDERR .

Did you notice that line 10 uses the object-oriented getline() method with STDIN ? This is a consequence of bringing in IO::Socket, which internally loads IO::Handle. As discussed in Chapter 2, a side effect of IO::Handle is to add I/O object methods to all filehandles used by your program, including the standard ones.

Unlike with the daytime client, we don't need to worry about what kind of line ends the echo service uses, because it echoes back to us exactly what we send it.

Note also that we did not need to set autoflush mode on the IO::Socket object, as we did in examples in Chapter 4. Since IO::Socket version 1.18, autoflush is turned on by default in all sockets created by the module. This is the version that comes with Perl 5.00503 and higher.


   
Top


Network Programming with Perl
Network Programming with Perl
ISBN: 0201615711
EAN: 2147483647
Year: 2000
Pages: 173

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