The IO::Socket module included in the core Perl distribution provides an object-oriented approach to socket programming. This module provides a convenient way to handle the large number of options you have to deal with, and it handles the laborious task of forming addresses. IO::Socket is built upon the Socket module provided in the standard library. It inherits from IO::Handle, which supports a class of filehandle objects for much of the IO library. The following IO::Socket functions are simply frontends for the corresponding built-in functions and use the same syntax:
Thesocket socketpair bind listen send recv peername (same as getpeername) sockname (same as getsockname)
acceptfunction in IO::Socket is slightly different from the equivalent function, however, and is described later in the chapter.
IO:Socket contains two subclasses: INET and UNIX. The INET subclass is used to create and manipulate Internet-domain sockets, such as the ones used in the examples. The UNIX subclass creates Unix domain sockets.
IO::Socket greatly simplifies the implementation of a socket for client communications. The following example creates an Internet-domain socket (using the INET subclass) and attempts to connect to the specified server:
use IO::Socket; $sock = new IO::Socket::INET (PeerAddr => 'www.ora.com', PeerPort => 80, Proto => 'tcp'); die "$!" unless $sock;
IO::Socket::INET::newcreates an object containing a socket filehandle and connects it to the host and port specified in
PeerPort. The object
$sockcan then be written to and read from like other socket filehandles.
On the server side, IO::Socket provides a nice wrapper for creating
server sockets. The wrapper encompasses
listen procedures, while creating a new
IO::Socket object. For example, we can create an Internet-domain socket with
The parameters for the new socket object determine whether it is a server or a client socket. Because we're creating a server socket,use IO::Socket; $sock = new IO::Socket::INET (LocalAddr => 'maude.ora.com', LocalPort => 8888, Proto => 'tcp', Listen => 5); die "$!" unless $sock;
LocalPortprovide the address and port to bind to the socket. The
Listenparameter gives the queue size for the number of client requests that can wait for an
acceptat any one time.
When communication is finished on both client and server sockets, they should be destroyed with$new_sock = $sock->accept();
close. If a socket is not properly closed, the next time you attempt to use a socket with the same name, the system will complain that the socket is already in use.
The following methods are defined in IO::Socket and can be used on socket objects of either the INET or UNIX class:
Specifies the remote host and optional port number for a client connection.
hostname can be either a name, like www.oreilly.com,
or an IP number of the form 18.104.22.168.
Type => SOCK_STREAM | SOCK_DGRAM
Reuse => 1
Whether a server (receiving) or client (requesting) socket is created depends
on the parameters provided to the constructor. If
Listen is defined, a
server socket is automatically created. If no protocol is specified, it is
derived from the service on the given port
number. If no port number is given,
tcp is used by default.
The IO::Socket::UNIX subclass creates a Unix-domain socket.
Unix-domain sockets are local to the current host and are used internally to
implement pipes, thus providing communication between unrelated processes.
sockets provides finer control than using named pipes, also
called FIFO (first-in, first-out) buffers. This is because
receiving sockets can distinguish between different client
connections, which can then be assigned to different sessions
The IO::Socket::UNIX constructor (
new()) creates the socket and returns an
object containing a filehandle. The constructor can take the following options:
Type => SOCK_STREAM | SOCK_DGRAM
The following methods can be used on an object created with IO::Socket::UNIX.