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
$| = ; 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 => ,
Reuse =>
) or die"ERROR in Socket Creation : $!\n"; print "SERVER Waiting for client connection on port 8888"; while()
{
# 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
$| = ; 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
$| = ; 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()
{
# read operation on the socket
$socket->recv($recieved_data,); #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
$| = ; 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();
$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:

How To: Perl TCP / UDP Socket Programming using IO::Socket::INET的更多相关文章

  1. JAVA之旅(三十二)——JAVA网络请求,IP地址,TCP/UDP通讯协议概述,Socket,UDP传输,多线程UDP聊天应用

    JAVA之旅(三十二)--JAVA网络请求,IP地址,TCP/UDP通讯协议概述,Socket,UDP传输,多线程UDP聊天应用 GUI写到一半电脑系统挂了,也就算了,最多GUI还有一个提示框和实例, ...

  2. c++ 网络编程(一)TCP/UDP windows/linux 下入门级socket通信 客户端与服务端交互代码

    原文作者:aircraft 原文地址:https://www.cnblogs.com/DOMLX/p/9601511.html c++ 网络编程(一)TCP/UDP  入门级客户端与服务端交互代码 网 ...

  3. TCP/UDP套接字 java socket编程实例

    网络协议七层结构: 什么是Socket? socket(套接字)是两个程序之间通过双向信道进行数据交换的端,可以理解为接口.使用socket编程也称为网络编程,socket只是接口并不是网络通信协议. ...

  4. tcp / udp 协议及其实现的socket

    一.tcp协议 1.1 基本知识 特点: 可靠,慢,全双工通信 建立连接时:三次握手 断开连接时:四次挥手 在建立起连接之后 发送的每一条信息都有回执 为了保证数据的完整性,还有重传机制 长连接:会一 ...

  5. 【Java TCP/IP Socket】Java NIO Socket VS 标准IO Socket

    简介 Java  NIO从JDK1.4引入,它提供了与标准IO完全不同的工作方式. NIO包(java.nio.*)引入了四个关键的抽象数据类型,它们共同解决传统的I/O类中的一些问题.    1. ...

  6. TCP/UDP 协议,和 HTTP、FTP、SMTP,区别及应用场景

    一.OSI 模型 OSI 模型主要作为一个通用模型来做理论分析,而TCP/IP 协议模型是互联网的实际通讯协议,两者一般做映射分析,以下不做严格区分和声明(好吧,比较懒): OSI 整个模型层次大致可 ...

  7. TCP/UDP的网络底层实现

    1.1Socket的使用背景 当我们在使用微信.玩游戏.收发邮件,以及用web浏览器上网时,底层的实现是TCP/UDP的协议,封装socket实现网络通信功能. 了解了网络通信的底层实现原理,在出现s ...

  8. TCP Socket Programming in Node.js

    TCP Socket Programming in Node.js Posted on October 26th, 2011 under Node.jsTags: Client, node.js, S ...

  9. .net学习笔记---tcp/udp/http/socket

    什么是TCP和UDP,以及二者区别是什么? TCP的全称为传输控制协议.这种协议可以提供面向连接的.可靠的.点到点的通信. UDP全称为用户数据报协议,它可以提供非连接的不可靠的点到多点的通信. 使用 ...

随机推荐

  1. 最新 Arduino 驱动 12接口/户外 LED显示屏/LED点阵屏/LED单元板

    起因 现有的驱动LED显示屏的资料,比较好的只有这个.但是它驱动的是08接口的室内显示屏,而我要驱动的是12接口的户外显示屏.两种屏幕的区别在于户外屏幕点阵比较稀疏,而且二者的扫描方式,驱动方式都不太 ...

  2. Symfony2 学习笔记之插件格式

    一个bundle类似于其它框架中的插件,但是比插件表现更好.它跟其它框架最主要的不同是在Symfony2中所有东西都是bundle,包括核心框架功能和你写的所有应用程序代码.Symfony2中,bun ...

  3. php 正则中的"i,m,s,x,e"分别表示什么

    i如果设定此修正符,模式中的字符将同时匹配大小写字母.m当设定了此修正符,“行起始”和“行结束”除了匹配整个字符串开头和结束外,还分别匹配其中的换行符的之后和之前.s如果设定了此修正符,模式中的圆点元 ...

  4. 选择下拉列表最大索引值 Select From List By Max Index

    Select是网页表单中较为常见的元素,在Selenium2Library 中也有相应关键字可以操作,比如: (1)通过指定索引选择 Name: Select From List By Index   ...

  5. js 判断字符是否以汉字开头

    javascript代码如下: var re = new RegExp("^[\u4e00-\u9fa5]"); if (re.test("aaa好")) { ...

  6. 三年程序学习之二:(对web初认识)

    接着上一篇讲,之后第二天我就来公司上班了,主要是前端,CSS+DIV,table,网站维护之类的,这样的日子过了将近3个星期,一直没什么进展,自己也学不到什么技术,不过我觉得CSS+DIV我算是基础的 ...

  7. 150个JS特效脚本

    收集了其它一些不太方便归类的JS特效,共150个,供君查阅. 1. simplyScroll simplyScroll这个jQuery插件能够让任意一组元素产生滚动动画效果,可以是自动.手动滚动,水平 ...

  8. 单源最短路径-Dijkstra算法

    1.算法标签 贪心 2.算法描述 具体的算法描述网上有好多,我觉得莫过于直接wiki,只说明一些我之前比较迷惑的. 对于Dijkstra算法,最重要的是维护以下几个数据结构: 顶点集合S : 表示已经 ...

  9. Python 最佳实践

    前言 对我来说,以前每次面试是我审视自己,检验自己的一种方式.每次准备面试,以及被面试官问住的时候才会发现,其实我python我学的还不够好.工作中也是,可以从其他的同事那里获得成长.但是我今天说的是 ...

  10. Java多线程学习总结--线程概述及创建线程的方式(1)

    在Java开发中,多线程是很常用的,用得好的话,可以提高程序的性能. 首先先来看一下线程和进程的区别: 1,一个应用程序就是一个进程,一个进程中有一个或多个线程.一个进程至少要有一个主线程.线程可以看 ...