How To: Perl TCP / UDP Socket Programming using IO::Socket::INET
5 stars based on
What is a socket? Just another bit of computer jargon? Devling a little into networking history, it is a Berkeley UNIX mechanism of creating a virtual duplex connection between processes. This was later ported on to every known OS enabling communication between systems across geographical location running on different OS software. If not for the socket, most of the network communication between systems would never ever have happened. Taking a closer look; a typical computer system on a network receives and sends information as desired by the various applications running on it.
This information is routed to the system, since a unique IP address is designated to it. On the system, this information is given to the relevant applications which listen on different ports. For example a net browser listens on port 80 for information. Also we can write applications which listen and send information on a specific port number. There are other types but this classification is fair enough to get started, a socket has a domain UNIX or interneta connection type connection oriented or connection less and a protocol TCP or UDP.
A connection oriented or a stream socket is a reliable two way communication. If you send three packets, say 1, 2 and 3, they are received in the same order they were sent. They achieve this high level of transmission quality by using TCP for error free reliable communication.
The ubiquitous telnet application uses stream sockets. They are connectionless, since the connection need not be open as in stream sockets, the packet formed is given a destination IP address and than transmitted. This method is mostly used for packet-to-packet transfer as in ftp applications. A packet is formed by encapsulating the data in a header at each level as it passes through the layers protocol stack.
At the receiving end the headers are stripped off as the packet travels up the layers to get the data. Basically, at each layer, the protocol adds a header to the payload to perform the required functionality.
Just about everything on the network deals with client processes talking to server processes and vice versa. Take the ubiquitous telnet, for instance. When you connect to a remote host on port 23 with telnet the clienta program on that host called telnetd, the server springs to life. It handles the incoming telnet connection, sets up a login prompt, etc. Note that the client-server pair can speak in streaming or stream-less, or anything else as long as they are speaking the same thing.
In this article, Rahul shows us how to create a client-server socket program in Perl and then demonstrates it by pinging the server. These different machines can be located anywhere on the network. To create a server, simply perform the following steps using the built-in Perl function indicated: Create a socket with socket. Bind the socket to a port address with bind. Listen to the socket at the port address with listen.
Accept client connections with accept. Establishing a client is even easier: Connect the socket to the remote machine with connect. A Simple Server 1. Recall that the steps for creating a server were to create a socket, bind it to a port, listen at the port and accept client connections. Line 1 and 4 It is generally a good idea to compile a Perl script using strict. When choosing a port for your server, pick one that is unused on your machine. Line 10 and 11 The socket is created using the socket function.
A socket is like a file handle-it can be read from, written to or both. The function setsockopt is called to ensure that the port will be immediately reusable. Line 16 The listen function causes the server to begin listening at the port. The second argument to the listen function is the maximum queue length or the maximum number of pending client connections.
Line 20 Once the server begins listening at the port, it can accept client connections using the accept function. The return value of the accept function is the Internet address of the client in a packed format. To convert the packed numeric address to a host name, the function gethostbyaddr is used. Start the script on a localhost. The output looks something like this. A Simple Client 1. Line 14 and 15 A socket is created using socket and the client connects the socket to the port address using connect.
Line 17 and 21 The while loop then reads the data the server sends to the client until the end-of-file is reached, printing this input to STDOUT. Then the socket is closed.
Output on the server: We shall see now, how it is possible. Output of the new cute scripts: Sample Ping Program 1. Ping makes pinging remote hosts easy and quick. An object of the class created. Here we use the icmp protocol you can also choose tcp or udp. Thu Feb 6 The freely available modules on CPAN including the modules for network programming cater to all development needs.
If you have any questions or comments about this article please post them here.