Perl socket编程

时间:2022-09-09 20:18:08

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.
本文讨论使用Perl内建的socket模块来实现Perl socket编程

This article covers the following topics:
本文的主题如下:
. Perl example code for TCP client and server             (Perl实现TCP客户端和服务端)
. Perl example code for UDP client and server             (Perl实现UDP客户端和服务端)
. Read and write descriptor list using Select(IO::Select) (使用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.
IO::Socket::INET用来执行socket操作,如创建,绑定,连接,监听,和关闭socket;
IO::Select模块用来获得可读/写操作的描述符;

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
Perl TCP 服务端操作
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                           (创建socket)
. Bind the socket to an address and port      (绑定socket的地址和端口)
. Listen to the socket at the port address    (监听端口)
. Accept the client connections               (接受连接)
. Perform read/write operation on the socket. (执行读写操作)

#!/usr/bin/perl
#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 => '127.0.0.1',
    LocalPort => '5000',
    Proto => 'tcp',
    Listen => 5,
    Reuse => 1
) or die "ERROR in Socket Creation : $!\n”;

print "SERVER Waiting for client connection on port 5000";

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

Also, refer to our earlier Perl debugger article to learn how to debug your perl code.

Perl TCP Client Operation
Perl TCP 客户端操作
The Perl code given below does the following:
. Create the socket.                                 (创建socket) 
. Connect to the remote machine at a specific port.  (连接服务端的端口)
. Perform read/write operation on the socket.        (执行读/写操作)

#!/usr/bin/perl
#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 => '127.0.0.1',
    PeerPort => '5000',
    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();

Note: You can use Vim editor as a Perl IDE using the perl-support.vim Plugin.

Perl UDP Server
Perl UDP 服务端
The Perl code given below does the following:
. Create the socket.                     (创建socket) 
. Bind the socket to the specific port.  (绑定端口)

#!/usr/bin/perl
#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 => '5000',
    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
Perl UDP 客户端
The Perl code given below does the following:
. Create the UDP client.                          (创建socket) 
. Connect to the specific UDP server.             (连接到指定UDP服务端)
. Perform write and read operation on the socket. (执行读/写操作)

#!/usr/bin/perl
#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   => '127.0.0.1:5000',
    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();

Also, refer to our earlier article to understand Perl Array Reference.

IO::Select Module – Get to know the list of ready descriptors
IO::Select Module – 获得准备好的描述符

IO::Select module provides following two major functions:
. can_read() returns the available read descriptors   (返回可读的描述符) 
. can_write() returns the available write descriptors (返回可写的描述符)

To understand the usage of IO::Select, we are going to use this module in the tcpserver code.

Step 1 : Create the IO::Socket object interface
创建 IO::Socket对象接口

The following code-snippet creates the object interface of IO::Socket::INET modules 
which internally creates a socket, binds and listens to a specified port address.

$socket = new IO::Socket::INET (
    LocalHost => '127.0.0.1',
    LocalPort => '5000',
    Proto => 'tcp',
    Listen => 5,
    Reuse => 1
) or die "ERROR in Socket Creation : $!\n";

$select = IO::Select->new($socket) or die "IO::Select $!";

Step 2 : Add descriptors to Select objects
将描述符添加到Select对象

The following code-snippet adds the descriptor to the list of select objects to get the descriptors ready.

@ready_clients = $select->can_read(0);
foreach my $fh (@ready_clients)  {
  print $fh "";
  if($fh == $socket) {
    my $new = $socket->accept();
    $select->add($new);
  }
}

Step 3: Get descriptors which are ready to read
获得可读的描述符

Following Perl code-snippets gets the list of descriptor that are ready to read.

@ready_clients = $select->can_read(0);
foreach my $fh (@ready_clients)  {
  if($fh != $socket)  {
    chomp($data=<$socket>);
    print $data,"\n";
  }
}

In the same way, we can do for the write operation on the socket.

Step 4: Remove Client Socket Descriptor from Select list
将客户端socket描述符从select列表中删除

When the connection is closed, you can remove the client socket descriptor for the select list as shown below.
当连接关闭后,需要将socket描述符从select列表中删除

SIGPIPE signal gets generated when we try to send/receive data on the socket that is closed by the remote machine. 
So, we can assign the signal handler for SIGPIPE signal, which should remove of descriptor from the select list as shown below.
在使用socket进行数据的发送/接收中,当远端机器关闭socket连接时,将会发送SIGIPE信号.
因此,我们可以在收到SIGIPE信号时,将描述符从select 列表中删除

# $current_client is the global variable which has the recent file descriptor
# on which the send/receive operation is tried.
### Handle the PIPE
$SIG{PIPE} =  sub
{
  ####If we receieved SIGPIPE signal then call Disconnect this client function
  print "Received SIGPIPE , removing a client..\n";
  unless(defined $current_client){
    print "No clients to remove!\n";
  }else{
    $Select->remove($current_client);
    $current_client->close;
  }

#print Dumper $Self->Select->handles;
  print "Total connected clients =>".(($Select->count)-1)."<\n";
};

Perl socket编程的更多相关文章

  1. 22-Perl Socket 编程

    1.Perl Socket 编程Socket又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求,使主机间或者一台计算机上的进程间可以通讯. ...

  2. socket编程的同步、异步与阻塞、非阻塞示例详解

     socket编程的同步.异步与阻塞.非阻塞示例详解之一  分类: 架构设计与优化 简介图 1. 基本 Linux I/O 模型的简单矩阵 每个 I/O 模型都有自己的使用模式,它们对于特定的应用程序 ...

  3. Go语言系列- Socket编程和Redis

    Socket编程 一.socket编程概述 什么是socket编程? socket编程是计算机PC机器上2个程序通过一个双向的通信连接实现数据的交互,这个连接的一端就是一个socket.socket的 ...

  4. Linux下的C Socket编程 -- server端的继续研究

    Linux下的C Socket编程(四) 延长server的生命周期 在前面的一个个例子中,server在处理完一个连接后便会立即结束掉自己,然而这种server并不科学啊,server应该是能够一直 ...

  5. java socket编程(li)

    一.网络编程中两个主要的问题 一个是如何准确的定位网络上一台或多台主机,另一个就是找到主机后如何可靠高效的进行数据传输.在TCP/IP协议中IP层主要负责网络主机的定位,数据传输的路由,由IP地址可以 ...

  6. Python Socket 编程——聊天室示例程序

    上一篇 我们学习了简单的 Python TCP Socket 编程,通过分别写服务端和客户端的代码了解基本的 Python Socket 编程模型.本文再通过一个例子来加强一下对 Socket 编程的 ...

  7. Linux下的C Socket编程 -- server端的简单示例

    Linux下的C Socket编程(三) server端的简单示例 经过前面的client端的学习,我们已经知道了如何创建socket,所以接下来就是去绑定他到具体的一个端口上面去. 绑定socket ...

  8. Linux下的C Socket编程 -- 获取对方IP地址

    Linux下的C Socket编程(二) 获取域名对应的IP地址 经过上面的讨论,如果我们想要连接到远程的服务器,我们需要知道对方的IP地址,系统函数gethostbyname便能够实现这个目的.它能 ...

  9. Linux下的C Socket编程 -- 简介与client端的处理

    Linux下的C Socket编程(一) 介绍 Socket是进程间通信的方式之一,是进程间的通信.这里说的进程并不一定是在同一台机器上也有可能是通过网络连接的不同机器上.只要他们之间建立起了sock ...

随机推荐

  1. android api汇集

    参考文章: 知乎-想写个 App 练手,有什么有趣的 API 接口推荐吗? 使用 Espresso 和 Dagger 测试网络服务 http://developer.simsimi.com/apps# ...

  2. 安装sql server managerment studio报错&quot&semi;The instance id is required but it is missing&quot&semi;

    问题描述: 今天在安装sql server managerment studio的时候提示报错"The instance id is required but it is missing&q ...

  3. 3&period;IP地址分类&lowbar;规划&lowbar;子网掩码

    IP地址分类_规划_子网掩码 3.1MAC地址 网卡的身份证号———MAC地址 MAC地址的长度为48位(6个字节),通常表示为12个16进制数,每2个16进制数之间用冒号隔开,如:08:00:20: ...

  4. Swift调用Objective-C

    Swift调用Objective-C需要一个名为“<工程名>-Bridging-Header.h”的桥接头文件,如下图所示.桥接头文件的作用是为Swift调用Objective-C对象搭建 ...

  5. ListControl常用操作汇总

    本文根据本人在项目中的应用,来谈谈CListCtrl的部分用法及技巧.当初学习时,查了很多资料,零零碎碎的作了些记录,现在主要是来做个总结,方便以后查阅.主要包括以下十三点内容:基本操作.获取选中行的 ...

  6. &lbrack;操作系统&rsqb;iOS6与iOS7屏幕适配技巧

    一.没有包装任何 导航控制器 或者 UITabBarController 1.控制器的view是UIScrollView\UITableView\UICollectionView时(控制器是UITab ...

  7. jz2440开发板设置备份

    ___________________uboot______________________________________ OpenJTAG> pribootdelay=2baudrate=1 ...

  8. 如何制作一个完美的全屏视频H5

    写在前面的话: 最近一波H5广告火爆整个互联网圈,身为圈内人,我们怎能     不! 知!道! :( 嘘!真不知道的也继续看下去,有收获 ↓ ) So,搞懂这个并不难. 这篇文章将带你从头到尾了解H5 ...

  9. Dropout

    参数正则化方法 - Dropout 受人类繁衍后代时男女各一半基因进行组合产生下一代的启发,论文(paper.pdf)提出了Dropout. Dropout是一种在深度学习环境中应用的正规化手段.它是 ...

  10. 西安活动 &vert; 9月15号 &quot&semi;拥抱开源&comma; 又见&period;NET&quot&semi; 线下交流活动

    随着.NET Core的发布和开源,.NET又重新回到了人们的视野.除了开源.跨平台.高性能以及优秀的语言特性,越来越多的第三方开源库也出现在了github上——包括ML.NET机器学习.Xamari ...