| Previous | Next
SocketsContents:Built-in Socket Functions
Why build networking functionality into your Perl scripts? You might want to access your email remotely, or write a simple script that updates files on an FTP site. You might want to check up on your employees with a program that searches for Usenet postings that came from your site. You might want to check a website for any recent changes, or even write your own home-grown web server. The network is the computer these days, and Perl makes network applications easy. Perl developers have their choice of modules for doing common tasks with network protocols; "SOAP" through "Usenet News" cover the modules for writing email, news, FTP, and web applications in Perl. If you can do what you want with the available modules, you're encouraged to jump to those chapters and skip this one. However, there will be times when you'll have to wrestle with sockets directly, and that's when this chapter comes in. Sockets are the underlying mechanism for networking on the Internet. With sockets, one application (a server) sits on a port waiting for connections. Another application (the client) connects to that port and says hello; then the client and server have a chat. Their actual conversation is done with whatever protocol they choose-for example, a web client and server would use HTTP, an email server would use POP3 and SMTP, etc. But at the most basic level, you might say that all network developing comes down to opening a socket, reading and writing data, and closing the socket. You can work with sockets in Perl at various levels. At the lowest level, Perl's built-in functions include socket routines similar to the system calls in C of the same name. To make these routines easier to use, the Socket module in the standard library imports common definitions and constants specific to your system's networking capabilities. Finally, the IO::Socket module provides an object interface to the socket functions through a standard set of methods and options for constructing both client and server communications programs. Sockets provide a connection between systems or applications. They can be set up to handle streaming data or discrete data packets. Streaming data continually comes and goes over a connection. A transport protocol such as TCP (Transmission Control Protocol) is used to process streaming data so that all of the data is properly received and ordered. Packet-oriented communication sends data across the network in discrete chunks. The message-oriented protocol UDP (User Datagram Protocol) works on this type of connection. Although streaming sockets using TCP are widely used for applications, UDP sockets also have their uses. Sockets exist in one of two address domains: the Internet domain and the Unix domain. Sockets used for Internet connections require the careful binding and assignment of the proper type of address dictated by the Internet Protocol (IP). These sockets are referred to as Internet-domain sockets. Sockets in the Unix domain create connections between applications either on the same machine or within a LAN. The addressing scheme is less complicated, often just providing the name of the target process. In Perl, sockets are attached to a filehandle after they have been created. Communication over the connection is then handled by standard Perl I/O functions. Built-in Socket FunctionsPerl provides built-in support for sockets. The following functions are defined specifically for socket developing. For full descriptions and syntax, see "Function Reference".
Regular functions that read and write filehandles can also be used for sockets, e.g., The socket functions tend to use hardcoded values for some parameters, which severely hurts portability. Perl solves this problem with a module called Socket, included in the standard library. Use this module for any socket applications that you build with the built-in functions (e.g., The next few sections describe Perl socket developing using a combination of the built-in functions together with the Socket module. After that, we describe the use of the IO::Socket module. Initializing a SocketBoth client and server use the
The Because this is a streaming connection using TCP, we specify The third argument indicates the protocol used for the connection. Each protocol has a number assigned to it by the system; that number is passed to Finally, if the socket call fails, the program will Client ConnectionsOn the client side, the next step is to connect with a server at a particular port and host. To do this, the client uses the The Continuing with the previous example, a call to
This call attempts to establish a network connection to the specified server and port. If successful, it returns true. Otherwise, it returns false and Assuming that the $data = "Hello"; send (FH, $data); The select (FH); print "$data"; To read incoming data from a socket, use either the recv (FH, $buffer); $input = <FH>; After the conversation with the server is finished, use Server ConnectionsAfter creating a socket with the
We start out by creating a socket for the server:
The filehandle A server-side socket must be bound to a port on the local machine by passing a port and an address data structure to the my $sin = sockaddr_in (80, INADDR_ANY); bind (FH, $sin) || die $!; The listen (FH, $length); The accept (NEW, FH) || die $!; Now the server can read and write to the filehandle Socket Module FunctionsThe following functions are imported from the Socket module for use in socket applications.
inet_aton (hostname) Translates a hostname such as www.anonymous.com or 18.181.0.24 into a data structure (a four-byte string) used for socket addresses. If the hostname cannot be resolved, the function returns the undefined value.
inet_ntoa (addr_string) Translates a four-byte address string (as returned by
Takes a port number and a four-byte
Takes one argument, a pathname, and returns the Unix domain socket address structure (the path packed in with AF_UNIX filled in). For Unix domain sockets, this structure is normally what you need for the arguments in
Takes a socket address structure and returns an array of two elements (in list context): the port number and the four-byte IP address.
Takes a Unix domain socket address structure (as returned by The following constants are defined in the Socket module:
|