Network Programming with Perl


 
Network Programming with Perl

By Lincoln  D.  Stein

Slots : 1

Table of Contents
Chapter  18.   The UDP Protocol

    Content

Using UDP Sockets with IO::Socket

Naturally, IO::Socket provides support for UDP sockets. To create a socket suitable for outgoing messages, call IO::Socket::INET->new() with a Proto argument of " udp " and no other arguments:

my $sock = IO::Socket::INET->new (Proto=>'udp') or die $@;

To create a socket bound to a known local port or interface address, provide one or more of the LocalAddr and LocalPort arguments:

my $sock = IO::Socket::INET->new (Proto => 'udp', LocalAddr => 12000, LocalPort => 'localhost' ) or die $@;

You may also connect() the socket and set a default destination address for send() by providing new() with the PeerAddr and optionally PeerPort arguments:

my $sock = IO::Socket::INET->new (Proto =>'udp', PeerAddr=>'wuarchive.wustl.edu:daytime(17)' ) or die $@;

IO::Socket implements both send() and recv() methods . They are wrappers around the eponymous built-in functions, with a few improvements. For one, the $flags argument is optional in both send() and recv() methods. (It is required in the built-in version.) In addition, the recv() call remembers the source address of the most recently received datagram. You may retrieve it using the peername() , peeraddr() , peerport() , and peerhost() methods.

$peer_addr = $socket->recv($data,$length [,$flags])

The recv() method removes the next available UDP datagram from the receive queue and stores up to $length bytes into $data . If successful, the method returns the packed address of the datagram's sender. Otherwise, it returns undef and sets $! to the error code. $flags has the same meaning as in the built-in function and defaults to if not specified.

$bytes = $socket->send($data [,$flags [,$dest_addr]])

The send() method sends the contents of $data via the socket, returning the number of bytes successfully enqueued. $flags and $dest_addr have the same meaning as in the built-in send() function. $flags is optional and, if not specified, defaults to 0. For connected sockets, $dest_addr should not be used.

As a convenience, if an unconnected socket has previously been used to receive a packet and if $dest_addr is not explicitly specified, the socket object uses this address as the default destination for send() .

Daytime Client Using IO::Socket

We will illustrate the object-oriented idiom by rewriting the daytime client to use IO::Socket. In addition, this example shows how a client can connect a UDP socket to make send() more convenient to use. The program, named udp_daytime_cli2.pl , is listed in Figure 18.2.

Figure 18.2. UDP daytime client using IO::Socket

Lines 1 “7: Set up script We load the IO::Socket module, bringing in the default socket constants and the constants related to line endings.

We set the input record terminator global to CRLF and read the destination host and port from the command line.

Lines 8 “10: Create socket We call IO::Socket::INET->new() to create a new socket. We specify a Proto of " udp ," overriding IO::Socket's defaults. In addition, we pass PeerHost and PeerPort arguments, causing new() to connect() the socket after creating it.

Lines 11 “12: Send request, receive response We call the socket's send() method to send a request. We then block in recv() until we get a response. If successful, the response is copied into $data .

Lines 13 “15: Print response We remove the CRLF from the response with chomp() and print it to standard output.

When we run the revised script, it works in the same way as the earlier version:

% udp_daytime_cli2.pl wuarchive.wustl.edu Thu Aug 17 11:00:30 2000


   
Top

Категории