Simple socket in Perl

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();


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值