Pages

Wednesday, April 25, 2018

Writing Client-Server Applications using IO::Socket

Introduction

You should
now know what a client and server program is. Simply put, a client
requests a service and a server services the request. There are
several ways to do this. The crudest will be to call a program within
perl and pass the parameters during the call. Another way is to use
pipes. A program can call another program but the input to the called
program will be passed through the STDIN of the other program. Both
methods are crude and allow only for simple passing of input and
output. Another way to do this is using sockets.

Sockets allow two programs to
communicate with each other. The server binds a port on the machine.
The client connects to that port to communicate with the server

IO:Socket Module

Perl
has a module that simplifies socket communication. It is called
IO::Socket. Simply put, the client has to connect to the server
socket. The server meanwhile has to get the port and listen to
anything coming in on that socket.

Server script

The server
is the program that listens to a request and processes the request.
For our exercise, we will write a simple server that accepts numbers
and adds all numbers up. (I know, you might as well do this in the
client program but this is just an illustration)
The script has to do the
following:

Open
the listening socket by specifying the protocol, local host address
and the port

Save this as server.pl.
Start a command window and run this. The output should show something
like this:
At your
service. Waiting...
This means that the server is
listening to any request on the given port.

Client Script

The client drives the process. It
sends a request to the server. It has to connect to the port the
server is listening to. It should then receive some form of input
from STDIN to send to the server. At the end, it sends an 'end'
message to the server.

To illustrate interactive
communication, the server will echo back anything it receives from
the client. The client then verifies this against what it sent. If it
does not match, then it is an error.
When the client receives and 'end'
from STDIN, it receives the result from the server and prints out the
result.

Save this as client.pl.
Start another command window and run this. Make sure that the
server.pl is running also.
T
he client should display
something like this:Connected
to 127.0.0.1 on port: 8081
Type in any set of numbers and
press <Enter&gteach time. When you are done, type end
and press <enter>. Look at the result. The server should
return the right computation.

Where can you use this?

The
server can take the place of say a tax calculation routine where you
send all the relevant numbers and at the end receive the calculated
tax.

The server can also be modified to
perform database access and return the result to the client. The
client can send a message like select tablename. The
server can then format the proper SQL statement to get the rows. With
this method, programmers on the client side need not worry about SQL
statements because the programmers with SQL skills can write the SQL
statements on the server side.