A Simple Network Client


 
Network Programming with Perl
By Lincoln  D.  Stein
Slots : 1
Table of Contents
Chapter  3.   Introduction to Berkeley Sockets

    Content

To put this information into context, we'll now write a client for the daytime service. This service, which runs on many UNIX hosts , listens for incoming connections on TCP port 13. When it sees such a connection, it outputs a single line of text with the current time and date.

The script gets the dotted IP address of the daytime server from the command line. When we run it on 64.7.3.43, which is the IP address of the wuarchive.wustl.edu software archive, we get output like this:

 %  daytime_cli.pl 64.7.3.43  Sat Jan 6 19:11:22 2001 

Figure 3.4 shows the code for the daytime client.

Figure 3.4. A daytime client

graphics/03fig04.gif

Lines 1 “3: Load modules We turn on strict type checking with use strict. This will avoid bugs introduced by mistyped variable names , the automatic interpretation of bare words as strings, and other common mistakes. We then load the Socket module, which imports several functions useful for socket programming.

Lines 4 “6: Define constants We now define several constants. DEFAULT_ADDR is the IP address of a remote host to contact when an address isn't explicitly provided on the command line. We use 127.0.0.1, the loopback address.

PORT is the well-known port for the daytime service, port 13, while IPPROTO_TCP is the numeric protocol for the TCP protocol, needed to construct the socket.

It's inelegant to hard code these constants, and in the case of IPPROTO_TCP may impede portability. The next section shows how to look up these values at run time using symbolic names.

Lines 7 “9: Construct the destination address The next part of the code constructs a destination address for the socket using the IP address of the daytime host and the port number of the daytime service. We begin by recovering the dotted-quad address from the command line or, if no address was specified, we default to the loopback address.

We now have an IP address in string form, but we need to convert it into packed binary form before passing it to the socket creation function. We do this by using the inet_aton() function, described earlier.

The last step in constructing the destination address is to create the sockaddr_in structure, which combines the IP address with the port number. We do this by calling the sockaddr_in() function with the port number and packed IP address.

Line 10: Create the socket The socket() function creates the communications endpoint. The function takes four arguments. The first argument, SOCK , is the filehandle name to use for the socket. Sockets look and act like filehandles, and like them are capitalized by convention. The remaining arguments are the address family, the socket type, and the protocol number. With the exception of the protocol, which we have hard-coded, all constants are taken from the socket module.

This call to socket() creates a stream-style Internet-domain socket that uses the TCP protocol.

Line 11: Connect to the remote host SOCK corresponds to the local communications endpoint. We now need to connect it to the remote socket. We do this using the built-in Perl function connect() , passing it the socket handle and the remote address that we built earlier. If connect() is successful, it will return a true value. Otherwise, we again die with an error message.

Line 12: Read data from remote host and print it We can now treat SOCK like a read/write filehandle, either reading data transmitted from the remote host with the read() or <> functions, or sending data to the host using print() . In this case, we use the angle bracket operator ( <> ) to read all lines transmitted by the remote host and immediately echo the data to standard output.

We discuss the socket() and connect () calls in more detail in Chapter 4.


   
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