设为首页 收藏本站
查看: 657|回复: 0

[经验分享] How To: Perl TCP / UDP Socket Programming using IO::Socket::INET

[复制链接]

尚未签到

发表于 2015-12-26 12:35:29 | 显示全部楼层 |阅读模式
  http://www.thegeekstuff.com/2010/07/perl-tcp-udp-socket-programming/
  In this article, let us discuss how to write Perl socket programming using the inbuilt socket modules in Perl.
  Perl socket modules provides an object interface that makes it easier to create and use TCP / UPD sockets.
  This article covers the following topics:


  • Perl example code for TCP client and server
  • Perl example code for UDP client and server
  • Read and write descriptor list using Select(IO::Select)
  
  CPAN module IO::Socket::INET is used to perform socket operations such as — creating, binding, connecting, listening and closing the socket.
  IO::Select module is used for obtaining the descriptors that are ready for read/write operations.

Perl TCP Client and Server
  TCP is a connection oriented networking protocol. In this example, let us review the Perl code-snippet that will explaining us the simple client and server communication.

Perl TCP Server Operation
  The socket operation such as socket creation, binding and listening to the socket is performed by the IO::Socket::INET module.
  The Perl code given below does the following:


  • Create the Socket
  • Bind the socket to an address and port
  • Listen to the socket at the port address
  • Accept the client connections
  • Perform read/write operation on the socket.



#!c:\perl\bin\perl.exe
#tcpserver.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 => '61.52.222.111',
LocalPort => '8888',
Proto => 'tcp',
Listen => 5,
Reuse => 1
) or die"ERROR in Socket Creation : $!\n";
print "SERVER Waiting for client connection on port 8888";
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 : $peeraddress:$peerport\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";
}
$socket->close();
  

Perl TCP Client Operation
  The Perl code given below does the following:


  • Create the socket.
  • Connect to the remote machine at a specific port.
  • Perform read/write operation on the socket.



#!c:\perl\bin\perl.exe
#tcpclient.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 => '61.52.222.111',
PeerPort => '8888',
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”;
print $socket "$data\n”;
# we can also send the data through IO::Socket::INET module,
# $socket->send($data);
sleep (10);
$socket->close();
  

Perl UDP Server
  The Perl code given below does the following:


  • Create the socket.
  • Bind the socket to the specific port.



#!c:\perl\bin\perl.exe
#udpserver.pl
use IO::Socket::INET;
# flush after every write
$| = 1;
my ($socket,$received_data);
my ($peeraddress,$peerport);
#  we call IO::Socket::INET->new() to create the UDP Socket and bound
# to specific port number mentioned in LocalPort and there is no need to provide
# LocalAddr explicitly as in TCPServer.
$socket = new IO::Socket::INET (
LocalPort => '8888',
Proto => 'udp',
) or die "ERROR in Socket Creation : $!\n";
while(1)
{
# read operation on the socket
$socket->recv($recieved_data,1024);
#get the peerhost and peerport at which the recent data received.
$peer_address = $socket->peerhost();
$peer_port = $socket->peerport();
print "\n($peer_address , $peer_port) said : $recieved_data";
#send the data to the client at which the read/write operations done recently.
$data = "data from server\n";
print $socket "$data";
}
$socket->close();
Perl UDP Client
  The Perl code given below does the following:


  • Create the UDP client.
  • Connect to the specific UDP server.
  • Perform write and read operation on the socket.



#!c:\perl\bin\perl.exe
#udpclient.pl
use IO::Socket::INET;
# flush after every write
$| = 1;
my ($socket,$data);
#  We call IO::Socket::INET->new() to create the UDP Socket
# and bind with the PeerAddr.
$socket = new IO::Socket::INET (
PeerAddr   => '61.52.222.111:8888',
Proto        => 'udp'
) or die "ERROR in Socket Creation : $!\n";
#send operation
$data = "data from client";
$socket->send($data);
#read operation
$data = <$socket>;
print "Data received from socket : $data\n ";
sleep(10);
$socket->close();
  
  
  http://www.tutorialspoint.com/perl/perl_socket_programming.htm
  
  To explain above mentioned socket concept we will take an example of Client - Server Programming using Perl.
  To complete a client server architecture we would have to go through the following steps:

To create a server


  •   Create a socket using socket call.

  •   Bind the socket to a port address using bind call.

  •   Listen to the socket at the port address using listen call.

  •   Accept client connections using accept call.


To create a client


  •   Create a socket with socket call.

  •   Connect (the socket) to the server using connect call.

  Following diagram shows complete sequence of the calls used by Client and Server to communicate with each other:
DSC0000.jpg
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-156549-1-1.html 上篇帖子: Perl调用外部命令的方式和区别 下篇帖子: perl 模板
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表