Hi friends,
you have been explained the initial part of perl
programming by Ameya.Here i am going to explain an important feature of
perl i.e. network programming, eg how to make programs for client-server
relationship.
socket: end points of a two way
communication path.
To make an application for client-server
relationship, you have to write one program for server & one for
client.
server program
Imp. steps are as:
- First you have to acquire a port which is not yet being used by any
other program by getservbyport function.
syntax:
(name,aliases,portnum,protoname)=getservbyport(searchportnumber,searchproto);
this
searches for the port searchportnumber in the /etc/services file
& if search is succesful it returns the values denoted on L.H.S. which is
a four element list otherwise empty list is returned.
eg.
$port=2000; while(getservbyport($port,"tcp")){port++;}
- Get the number
of the protocol you want to use by using getprotobyname function.
syntax:
(name,aliases,number)=getprotobyname(searchname)
It searches for the protocol by name searchname in the
/etc/protocols
file
& if search is succesful it returns the values denoted on L.H.S. which is
a three element list otherwise empty list is returned.
eg
($d1,$d2,$prototype)=getprotobyname("tcp");
- Get
the name of host using gethostbyname function & giving the
name of your machine(string) as
its argument & if you don't know the name of your machine then you can
give UNIX command hostname as argument(i.e. 'hostname' , single
quotes are given to execute the UNIX command hostname).
syntax:
(name,altnames,addrtype,len,addrs)=gethostbyname(hname);
It
is similar to above two commands except it searches for machine name in
/etc/hosts file.
($d1,$d2,$d3,$d4,$rawserver)=gethostbyname('hostname');
- you have to create a socket which will act as
server socket. use socketfunction for this purpose.
syntax:
socket(socketname,domain,type,format);
eg
socket(SSOCKET,2,1,$prototype);