Main Page Content
Socket Programming In Perl
Using sockets in Perl, it is possible to communicate to a server using a client program. Here, I will explain how using sockets a client program can communicate to a server. The client program is run at the client-side while the server program runs in the Internet. For testing the program, you can run it on a local server.
For this example, I will use the IO::Socket::INET Perl module which is included with most Perl distributions. If you don't have the module, get it from http://www.cpan.org/modules/index.html.
We will use the User Datagram Protocol (UDP) to connect to the server. The module also supports the TCP protocol.
Client Program :
First of all we need to create the socket. The socket is created easily by initializing a new instance of the IO::Socket::INET object like this :
use IO::Socket::INET;$MySocket=new IO::Socket::INET->new(PeerPort=>1234, Proto=>'udp', PeerAddr=>'localhost');MySocket, thus inherits all the properties and methods of the IO::Socket::INET object.
The various key-value pairs we have passed to the newly created object are as follows :
- PeerPort = 1234; The PeerPort can be a number or symbolic service name.
- Proto = 'udp'; We use the UDP protocol. The TCP protocol is also supported. If Proto is not provided, it will try to derive it from the service name (PeerPort). If this fails, 'tcp' is assumed as the Proto.
- PeerAddr = 'localhost'. The PeerAddr can be a host name or an IP address. Here we are tesing the programs locally so we have used 'localhost'. If the server program is running in the Internet then you must use it's IP address or domain name.
Now that we have created the socket, we can now send a message to the server using the object's send() method :
$msg="This is the message";$MySocket->send($msg);Thus we have created the client program. Now let us create the server program, that will display the messages that the client has sent.
Server Program :
In the server program also, we create the socket in a similar manner :
use IO::Socket::INET;$MySocket=new IO::Socket::INET->new(LocalPort=>1234, Proto=>'udp')Notice that in the server program we have passed only two key-value pairs :
- LocalPort = 1234; this is the local host bind port and should be same as the PeerPort value passed in the client program.
- Proto = 'udp'; Protocol used in communication with client, it should be same as the Proto value passed in the client program.
$MySocket->recv($text,128);print "Received message '", $text,"'";Here the first argument passed to the recv() method must be '$text', the second argument passed indicates the maximum number of bytes the server should receive.
After receiving the message, it can be easily displayed using the $text variable.
Thus we have created the server program. Now the client can communicate with server using a socket. Note that for the server to receive messages from the client, it (the server) should be running while the message is being sent by the client.
Client program code :
# Client Programuse IO::Socket::INET;print ">> Client Program <<";We have refined the client program so that it accept inputs from the keyboard which is transmitted to the server. A double return (pressing enter/return twice) will exit the client program.# Create a new socket
$MySocket=new IO::Socket::INET->new(PeerPort=>1234,Proto=>'udp',PeerAddr=>'localhost');# Send messages
$def_msg="Enter message to send to server : ";print "",$def_msg;while($msg=<STDIN>){ chomp $msg; if($msg ne '') { print "Sending message '",$msg,"'"; if($MySocket->send($msg)) { print ".....<done>",""; print $def_msg; } } else { # Send an empty message to server and exit $MySocket->send(''); exit 1; }}
Server program code :
# Server Programuse IO::Socket::INET;print ">> Server Program <<";We have refined the server program so that as and when the client sends a message, the server keeps displaying it. When the client sends a blank message, the server program terminates.# Create a new socket
$MySocket=new IO::Socket::INET->new(LocalPort=>1234,Proto=>'udp');# Keep receiving messages from client
$def_msg="Receiving message from client.....";while(1){ $MySocket->recv($text,128); if($text ne '') { print "Received message '", $text,"'"; } # If client message is empty exit else { print "Cilent has exited!"; exit 1; }}
Thus we have learned how to communicate to a server from a client using sockets.
© 2002 Premshree Pillai.