Holiday Assignment(Perl)

Only available on StudyMode
  • Download(s) : 68
  • Published : December 7, 2012
Open Document
Text Preview
Holiday Assignment
-Sockets in PERL
Holiday Assignment
-Sockets in PERL

Submitted to:-
S.S.Paul Sir
Submitted by:-
Rahul Singh
Reg No- 20098023(IT-2)
Submitted to:-
S.S.Paul Sir
Submitted by:-
Rahul Singh
Reg No- 20098023(IT-2)
by Rahul Singh
This report is a brief overview of the use of Perl in networking and communicating between servers and clients. It explains sockets and modules associated to them in Perl. Additionally, it also gives a brief idea about a Software Requirement Specification (SRS), its meaning, purpose and contents.

by Rahul Singh
This report is a brief overview of the use of Perl in networking and communicating between servers and clients. It explains sockets and modules associated to them in Perl. Additionally, it also gives a brief idea about a Software Requirement Specification (SRS), its meaning, purpose and contents.

SOCKET PROGRAMMING IN PERL
What is a Socket?
Sockets are a mechanism that allows programs to communicate, either on the same machine or across a network. The way it works is pretty simple: Each machine on a network is identified by some address. In tcp/ip networking a network address implies an IP address. (like 192.168.4.4) Apart from the IP address that specifies a machine, each machine has a number of ports that allow handling multiple connections simultaneously.

How does communication using sockets work?
A program that wishes to receive a connection from another program, asks the operating system to create a socket and bind it to some port. Then the program sits and listens on the socket it has created to receive incoming connections. The other program also creates a socket for communicating with the receiver. The caller needs to specify the IP address and the port number of the receiving end. The two programs may exchange information, each by writing to and reading from the socket it has created. How do sockets apply to Perl programs?

Perl provides support for the socket API natively. Perl socket modules provide an object interface that makes it easier to create and use TCP / UPD sockets. Socket programming in Perl, as with any other language, has two sides to it- the server (receiver) and the client (sender). The Server: The socket operation such as socket creation, binding and listening to the socket is performed by the IO::Socket::INET module. The first thing to do is to create a socket. The code for doing that is as given- use IO::Socket;

my $sock = new IO::Socket::INET (
LocalHost => 'thekla',
LocalPort => '7070',
Proto => 'tcp',
Listen => 1,
Reuse => 1,
);
die "Could not create socket: $!\n" unless $sock;
The program needs to specify the local hostname and the port to which the socket will be bound.  if the port is already in use this call will fail.  The 'Listen' variable specifies the maximum number of connections that can be queued by the socket waiting to accept. Its value 1 here means the queue can be 1 connection long. The 'Reuse' variable tells the system to allow reuse of the port after the program exits. This is to ensure that if the program exits abnormally and does not properly close the socket, running it again will allow opening a new socket on the same port. To wait for a connection, the accept() method is used, which will return a new socket through which we can communicate with the calling program. Information exchange is achieved by reading/writing on the new socket. The socket can be treated like a regular filehandle. my $new_sock = $sock->accept();

while(<$new_sock>) {
print $_;
}
close($sock);
The Client: At the other side of the communication, meaning the client side, all we need to do is to create a socket specifying the remote address and port. The...
tracking img