Perl io:socket io:select Server Client

Source: Internet
Author: User

    1. Server program

Accept client information and send a response

#!/usr/bin/perl-w

# socket_server.pl

Use strict;

Use Io::socket;

Use Io::select;


# hash to install IP Port


My ($ser _addr, $ser _port) = ("127.0.0.1", "12345");

Our ($buffer, $len);


My $socket = Io::socket::inet->new (

LOCALADDR = "$ser _addr", #本机IP地址

LocalPort = "$ser _port", #定义本机的Port, then bind

Type = Sock_stream, #套接字类型

Proto = "TCP", #协议名

Listen = #定义listen的最大数

Blocking = 0, #非阻塞

) or die "Can not create socket [email protected]";

My $sel = Io::select->new ($socket);

while (my @ready = $sel->can_read) {

foreach my $fh (@ready) {

if ($fh = = $socket) {

My $new = $socket->accept ();

$sel->add ($new);

}

else {

$len = $fh->recv ($buffer, 1024,0); #接收客户端消息

print "$buffer \ n";

$FH->send ("Server ok!\n", 0); #发送服务端消息

$FH->autoflush (1);

$sel->remove ($FH);

$FH->close ();

}

}

}

$socket->close () or warn "close socket [email protected]";


2.client

Connect the client, send a message, and accept the server-side reply message


#!/usr/bin/perl-w

# socket_client.pl

Use strict;

Use Io::socket; # #IO:: Socket::inet module is a sub-module of the Io::socket module without re-use.

Use Io::select; # #该模块和Linux下select () functions are implemented in a consistent and extended function. can be viewed Perldoc.


For (my $i =0; $i <20000; $i + +) {

&send_rev_data;


}


Sub send_rev_data{

My ($ser _addr, $ser _port) = ("127.0.0.1", "12345");

# #IO:: Socket::inet->new () Initializes a socket connection with the ability to integrate sockets, Inet_aton, connect, bind, listen, and more. You do not need to separate the IP address into the network address structure, the direct use of the IP address is OK.

# #具体参数下面单独介绍.

My $socket = Io::socket::inet->new (

peeraddr = "$ser _addr",

Peerport = "$ser _port",

Type = Sock_stream,

Proto = "TCP",

) or die "Can not create socket [email protected]";

$socket->send ("Client ok!\n", 0); # #发送消息至服务器端.

$socket->autoflush (1);

My $sel = Io::select->new ($socket); # #建立select对象

while (my @ready = $sel->can_read) {# #等待服务端返回的消息

foreach my $fh (@ready) {

if ($fh = = $socket) {

while (< $fh >) {

Print $_;

}

$sel->remove ($FH);

Close $fh;

}

}

}

# $socket->close () or Die "close socket [email protected]";

}



This article is from the "Yiyi" blog, make sure to keep this source http://heyiyi.blog.51cto.com/205455/1598532

Perl io:socket io:select Server Client

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.