Socket is very useful in the communication between the processes in the same computer or in the network.
This is a simple example of a socket between 2 processes in Perl. If you want to have a look at the effects, you can first start the provider.java and then start therequester.java.
Usefullinks:
http://www.tutorialspoint.com/perl/perl_socket.htm
Code:
Provider.pl
#!/usr/bin/perl
#provider.pl
use IO::Socket::INET;
# flush after every write
$| = 1;
my ($socket,$client_socket);
my ($peeraddress,$peerport);
# creating object interface of IO::Socket::INET modules which internally does
# socket creation, binding and listening at the specified port address.
$socket = new IO::Socket::INET (
LocalHost => '127.0.0.1',
LocalPort => '2013',
Proto => 'tcp',
Listen => 5,
Reuse => 1
) or die "ERROR in Socket Creation : $!\n";
print "SERVER Waiting for client connection on port 5000\n";
while(1)
{
# waiting for new client connection.
$client_socket = $socket->accept();
# get the host and port number of newly connected client.
$peer_address = $client_socket->peerhost();
$peer_port = $client_socket->peerport();
print "Accepted New Client Connection From : $peer_address, $peer_port \n ";
# write operation on the newly accepted client.
$data = "DATA from Server";
print $client_socket "$data\n";
# we can also send the data through IO::Socket::INET module,
# $client_socket->send($data);
# read operation on the newly accepted client
$data = <$client_socket>;
# we can also read from socket through recv() in IO::Socket::INET
# $client_socket->recv($data,1024);
print "Received from Client : $data\n";
$num++;
# read operation on the newly accepted client
$data = <$client_socket>;
# we can also read from socket through recv() in IO::Socket::INET
# $client_socket->recv($data,1024);
print "Received from Client : $data\n";
$num++;
# read operation on the newly accepted client
$data = <$client_socket>;
# we can also read from socket through recv() in IO::Socket::INET
# $client_socket->recv($data,1024);
print "Received from Client : $data\n";
$num++;
# Data process in perl and write back to Java
print $client_socket "received message number: $num\n";
print"received message number: $num\n";
}
$socket->close();
Requester.pl
#!/usr/bin/perl
#requester.pl
use IO::Socket::INET;
# flush after every write
$| = 1;
my ($socket,$client_socket);
# creating object interface of IO::Socket::INET modules which internally creates
# socket, binds and connects to the TCP server running on the specific port.
$socket = new IO::Socket::INET (
PeerHost => '127.0.0.1',
PeerPort => '2013',
Proto => 'tcp',
) or die "ERROR in Socket Creation : $!\n";
print "TCP Connection Success.\n";
# read the socket data sent by server.
$data = <$socket>;
# we can also read from socket through recv() in IO::Socket::INET
# $socket->recv($data,1024);
print "Received from Server : $data\n";
# write on the socket to server.
$data = "DATA from Client: Hello server";
print $socket "$data\n";
# we can also send the data through IO::Socket::INET module,
# $socket->send($data);
#repete several times
# write on the socket to server.
$data = "DATA from Client: Hello server";
print $socket "$data\n";
# write on the socket to server.
$data = "DATA from Client: Hello server";
print $socket "$data\n";
# write on the socket to server.
$data = "DATA from Client: Hello server";
print $socket "$data\n";
# read the socket data sent by server.
$data = <$socket>;
# we can also read from socket through recv() in IO::Socket::INET
# $socket->recv($data,1024);
print "Received from Server : $data\n";
sleep (10);
$socket->close();