Other Socket-Related Functions


 
Network Programming with Perl
By Lincoln  D.  Stein
Slots : 1
Table of Contents
Chapter  4.   The TCP Protocol

    Content

Other Socket- Related Functions

In addition to the functions we have already seen, there are three Perl built-in functions related to sockets: send() , recv() , and socketpair() . We will use send() and recv() in later chapters of this book when we discuss TCP urgent data (Chapter 17), and the UDP protocol (Chapters 17 “20).

$bytes = send (SOCK,$data,$flags[,$destination])

The send() function uses the socket indicated by the first argument to deliver the data indicated by $data , to the destination address indicated by $destination . If the data was successfully queued for transmission, send() returns the number of bytes sent; otherwise , it returns undef . The third argument, $flags , is the bitwise OR of zero or more of the two options listed in Table 4.3.

We discuss the MSG_OOB flag in detail in Chapter 17. MSG_DONTROUTE is used in routing and diagnostic programs and is not discussed in this book. In general, you should pass 0 as the value of $flags in order to accept the default behavior.

If the socket is a connected TCP socket, then $destination should not be specified and send() is roughly equivalent to syswrite() . With UDP sockets, the destination can be changed with every call to send() .

$address = recv (SOCK,$buffer,$length,$flags)

The recv() function accepts up to $length bytes from the indicated socket and places them into the scalar variable $buffer . The variable is grown or shrunk to the length actually read. The $flags argument has the same significance as the corresponding argument in $send and should usually be set to 0.

If successful, recv() returns the packed socket address of the message sender. In case of error, the function returns undef and sets $! appropriately.

When called on a connected TCP socket, recv() acts much like sysread () , except that it returns the address of the peer. The real usefulness of recv() is for receiving datagrams for UDP transmissions.

$boolean = socketpair (SOCK_A,SOCK_B,$domain,$type,$protocol)

The socketpair() function creates two unnamed sockets connected end to end. $domain , $type , and $protocol have the same significance as in the socket() function. If successful, socketpair() returns true and opens sockets on SOCK_A and SOCK_B .

Table 4.3. send() Flags
Option Description
MSG_OOB Transmit a byte of urgent data on a TCP socket.
MSG_DONTROUTE Bypass routing tables.

The socketpair() function is similar to the pipe() function that we saw in Chapter 2, except that the connection is bidirectional. Typically a script creates a pair of sockets and then fork() , with the parent closing one socket and the child closing the other. The two sockets can then be used for bidirectional communication between parent and child.

While in principle socketpair() can used for the INET protocol, in practice most systems only support socketpair() for creating UNIX-domain sockets. Here is the idiom:

 socketpair(SOCK1,SOCK2,AF_UNIX,SOCK_STREAM,PF_UNSPEC) or die $!; 

We show examples of using UNIX-domain sockets in Chapter 22.

End-of-Line Constants Exported by the Socket Module

In addition to the constants used for constructing sockets and establishing outgoing connections, the Socket module optionally exports constants and variables that are useful for dealing with text-oriented network servers.

As we saw in Chapter 2, operating systems have different ideas of what constitutes the end of line in a text file, with various OSs using carriage return (CR), linefeed (LF), or carriage return/linefeed (CRLF). Adding to the confusion is the fact that Perl's " \r " and " \n " string escapes translate into different ASCII characters depending on the local OS's idea of the newline.

Although this is not a hard and fast rule, most text-oriented network services terminate their lines with the CRLF sequence, octal " \015\012 ". When performing line-oriented reads from such servers, you should set the input record separator, global $/ , to " \015\012 " (not to " \r\n ", because this is nonportable). To make this simpler, the Socket module optionally exports several constants defining the common line endings (see Table 4. 4). In addition, to make it easier to interpolate these sequences into strings, Socket exports the variables $CRLF , $CR , and $LF .

Table 4.4. Constants Exported by Socket Module
Name Description
CRLF A constant that contains the CRLF sequence
CR A constant that contains the CR character
LF A constant that contains the LF character

These symbols are not exported by default, but must be brought in with use either individually or by importing the " :crlf " tag. In the latter case, you probably also want to import the " :DEFAULT " tag in order to get the default socket-related constants as well:

 use Socket qw(:DEFAULT :crlf); 

   
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