How To: Perl TCP / UDP Socket Programming using IO::Socket::INET

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:

How To: Perl TCP / UDP Socket Programming using
IO::Socket::INET,码迷,mamicode.com

How To: Perl TCP / UDP Socket Programming using
IO::Socket::INET

时间: 2024-08-02 15:11:16

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

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

网络协议七层结构: 什么是Socket? socket(套接字)是两个程序之间通过双向信道进行数据交换的端,可以理解为接口.使用socket编程也称为网络编程,socket只是接口并不是网络通信协议. HTTP协议和Socket的区别 http协议是应用层,其模式是请求-应答,客户端发送请求,服务器端进行响应.传输的数据是原始格式的数据,eg :json.xml.text等数据格式. socket不是协议是接口,socket提供TCP/UDP socket 的实例,供java 或者其他语言操作数

tcp / udp 协议及其实现的socket

一.tcp协议 1.1 基本知识 特点: 可靠,慢,全双工通信 建立连接时:三次握手 断开连接时:四次挥手 在建立起连接之后 发送的每一条信息都有回执 为了保证数据的完整性,还有重传机制 长连接:会一直占用双方的端口 IO(input,output)操作,输入和输出是相对内存来说的 write send - output read recv - input 能够传递的数据长度几乎没有限制 应用场景: 文件的上传下载 发送邮件,网盘,缓存电影等 简述三次握手和四次挥手 三次握手 accept接受过

2020-03-21 TCP/UDP协议

一.tcp协议 如果在面试的过程中,要讲这个,可以拿打电话这个场景来说明. 二.UDP协议 用处:视频.直播.日志上报. 三.TCP/UDP的比较 四.Socket 原文地址:https://www.cnblogs.com/mathlin/p/12539800.html

TCP,UDP,HTTP,IP,SOCKET

近日对各网络协议进行了一番学习,宏观认识上有收获. 网络由下往上分为物理层.数据链路层.网络层.传输层.会话层.表示层和应用层.(引用)IP 协议对应于网络层,TCP/UDP协议对应于传输层, HTTP协议对应于应用层, SOCKET则是对TCP/IP协议的封装和应用. TCP连接的三次握手:第一次握手:客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确认: 第二次握手:服务器收到syn包,必须确认客户的SYN(ack=j+1),同时自己也发送一个SYN包(syn

iOS socket TCP UDP

TCP: 服务器: #import <Foundation/Foundation.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int main(int argc, const char * argv[]) { @autoreleasepool { // 1 int err; int fd=socket(AF_INET, SOCK_STREAM , 0);

Java TCP/UDP socket 编程流程总结

最近正好学习了一点用java socket编程的东西.感觉整体的流程虽然不是很繁琐,但是也值得好好总结一下. Socket Socket可以说是一种针对网络的抽象,应用通过它可以来针对网络读写数据.就像通过一个文件的file handler就可以都写数据到存储设备上一样.根据TCP协议和UDP协议的不同,在网络编程方面就有面向两个协议的不同socket,一个是面向字节流的一个是面向报文的. 对socket的本身组成倒是比较好理解.既然是应用通过socket通信,肯定就有一个服务器端和一个客户端.

HTTP,FTP,TCP,UDP及SOCKET

一.TCP/IP协议简析TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层:网络层:IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议传输层:TCP协议与UDP协议应用层:FTP.HTTP.TELNET.SMTP.DNS等协议 HTTP是应用层协议,其传输都是被包装成TCP协议传输.可以用SOCKET实现HTTP.SOCKET是实现传输层协议的一种编程API,可以是TCP,也可以是UDP. 二.Socket连接与HTTP连接区别[Socket]由于通常情况下Socket

linux socket TCP UDP bind 同义IP和port

//TCP and UDP can bind to the same IP & port. #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <unistd.h> #include &

Socket 通信原理(Android客户端和服务器以TCP&amp;&amp;UDP方式互通)

ZERO.前言 有关通信原理内容是在网上或百科整理得到,代码部分为本人所写,如果不当,还望指教. 一.Socket通信简介 Android与服务器的通信方式主要有两种,一是Http通信,一是Socket通信.两者的最大差异在于,http连接使用的是“请求—响应方式”,即在请求时建立连接通道,当客户端向服务器发送请求后,服务器端才能向客户端返回数据.而Socket通信则是在双方建立起连接后就可以直接进行数据的传输,在连接时可实现信息的主动推送,而不需要每次由客户端想服务器发送请求. 那么,什么是s