Sockets Programming
Jeff Williams
November 24, 1999
Basics
This presentation assumes you are
already familiar with the basics of how the Internet works and an understanding
of networks. If you wish to review the basics of networking, I've included some
information from O'Rielly's text called Network Primer
found in Advanced Perl Programming. I have also included links to Power
Point presentations given to me from a Grand Valley State Univeristy professor
on TCP/IP and Sockets
programing. We will cover the details on how in Perl you can write your own
Internet server and client programs.
I must highly recommend that if you
wish to expand your knowledge of sockets programming with Perl, read the following
chapters from these O'Rielly books:
Programming Perl, 6.2.4 Sockets
and the various functions found in Chapter 3
Advanced Perl Programming, 12.1 Networking
The Perl Cookbook, 17.0 Introduction
Perl provides native support for sockets and a module called Socket to smooth some of the rough edges associated with the native socket call. Perl itself simply provides an interface to the actual C commands the build and works the sockets. It turns out that there are still a large number of options to deal with, and since most applications use a fairly standard set of options, we instead use a truly convenient module called IO::Socket, which is built on Socket. This will allow us to write both TCP and UDP client and servers.
Simple
Clients and Servers
Sockets, you've heard this term before but what is it. Would you believe that
you use sockets everyday as you surf the Web or send and E-mail? A socket is
simply a pipe setup between an application, the Kernel, and a port. The socket
is the pipe in which any messages from your application can be sent and received.
Lets take a look at how Perl works with Sockets.
The following simple code allows us to examine how a sockets client/server program can work within Perl.
First, lets
take a look at the UDP code for client-server communication.
UDP client code
UDP server code
UDP works by passing a connectionless datagram from the client, to the server,
and back to the client. In this example, we are send a text string from the
client to the server. The server prints the message and sends it back to the
client. We can also send a message which shuts down the just echoing the text
sent by the client to the server. Note that we "use IO::Socket". This
is the simple package which hides all the complexity of sockets programming
from us. It is really provides a wrapper around the C code which really does
this work. We then open a UDP socket assigned to a handle called $server. With
this handle, we can then receive and send message to a client.
Now, lets look at an example of TCP code.
TCP
client code
TCP server code
Here, the client sends a request for a file, and the server streams the information back. Since TCP establishes a connection, we are guaranteed an ordered return of data so we can simulate an FTP process. Note the differences between UDP and TCP and how messages are sent and received.
If you are concerned about performance, you can make more direct calls to the Socket package. Here is the TCP server code using the more detailed and direct approach (those familiar with C will notice similarities).
And of course you could code directly in C. This is a bit more complicated but doable. Here is an example of some C code I wrote for another project.
Conclusion
Finally, we can really make things fun by exploring all the different ways to write servers. We can build forking servers which spawn child processes to handle all the incoming requests. There are pre-forking servers which spawn a number of child processes before any requests come in and then servers off the request to an available child thread. There are non-blocking, blocking, and Multi-homed servers. And of course, you can create your own Daemon server!