| Previous | Next
The IO::Socket ModuleThe IO::Socket module included in the core Perl distribution provides an object-oriented approach to socket developing. 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: socket socketpair bind listen send recv peername (same as getpeername) sockname (same as getsockname) The IO:Socket contains two subclasses: INET and UNIX. The INET subclass is used to create and manipulate Internet-domain sockets, such as those used in the examples. The UNIX subclass creates Unix domain sockets. Client-Side SocketsIO::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;
Server-Side SocketsOn the server side, IO::Socket provides a nice wrapper for creating server sockets. The wrapper encompasses the use IO::Socket; $sock = new IO::Socket::INET (LocalAddr => 'maude.ora.com', LocalPort => 8888, Proto => 'tcp', Listen => 5); die "$!" unless $sock; The parameters for the new socket object determine whether it is a server or a client socket. Because we're creating a server socket, When the server receives a client request, it calls the $new_sock = $sock->accept( ); When communication is finished on both client and server sockets, they should be destroyed with IO::Socket MethodsThe following methods are defined in IO::Socket and can be used on socket objects of either the INET or UNIX class.
accept ([pkg]) Performs the
protocol Returns the protocol number for the protocol being used on the socket, if known. If the protocol is unknown, as with an AF_UNIX socket, returns
sockdomain Returns the number representing the socket address domain. For example, an AF_INET socket has the value
sockopt (opt, [val]) Sets and retrieves socket option
socktype Returns the number representing the socket type. For example, a SOCK_STREAM socket has the value
timeout ([val]) Sets or retrieves the timeout value associated with a socket. Without an argument, the current value is returned. If a timeout of IO::Socket::INET ReferenceAn Internet-domain socket is created with the
Whether a server (receiving) or client (requesting) socket is created depends on the parameters provided to the constructor. If IO::Socket::INET methodsThe following methods can be used on socket filehandle objects created by IO::Socket::INET.
peeraddr Returns the address part (packed string) of the socket-address data structure for the remote host to which a socket connects.
peerhost Returns the remote host address in the dotted-quad string form, e.g., 207.44.27.2.
peerport Returns the port number for the remote host to which a socket connects.
sockaddr Returns the address part (as a packed string) of the socket-address data structure for the socket.
sockhost Returns the address part of the socket-address data structure in the dotted-quad string form, e.g., 207.44.27.2.
sockport Returns the local port number for the socket. IO::Socket::UNIX ReferenceThe 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. Using 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 with the The IO::Socket::UNIX constructor (
The following methods can be used on an object created with IO::Socket::UNIX.
hostpath Returns the pathname to the local FIFO buffer.
peerpath |