博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Perl]IO::Socket实际应用
阅读量:4140 次
发布时间:2019-05-25

本文共 1961 字,大约阅读时间需要 6 分钟。

远程获取其他主机信息,适于远程主机管理(实际应用时添加必要的验证、加密、端口过滤等内容)。

server1:   srv1200read.pl  zclient2200.pl

server45: srv2200read.pl

server1监听1200端口,server45监听2200端口

server1向server45的2200端口发送消息,server45处理后向server1的1200端口返回处理后内容。

#!/usr/bin/perl -w
##srv1200read.pl 
use IO::Socket;
use IO::Select;
$socketread = new IO::Socket::INET (LocalHost => 'server45',
                              LocalPort => 1200,
                              Proto     => 'tcp',
                              Listen    => 5,
                              Reuse     => 1,
                             );
$read_set = new IO::Select;
$read_set->add($socketread);
while (1) {


        ($new) = IO::Select->select($read_set, undef, undef, undef);
        foreach $sock (@$new) {

                if ($sock == $socketread) {

print "########################################## ";
                        while ($new_sock=$sock->accept()) {

                                while (defined ($buf=<$new_sock>)) {

                                        #receive and control $buf
                                        print $buf;
                                }
                        }

                }
        }
}

###########################################################################

#!/usr/bin/perl -w
##srv2200read.pl 
use IO::Socket;
use IO::Select;
$socketread = new IO::Socket::INET (LocalHost => 'server1',
                              LocalPort => 2200,
                              Proto     => 'tcp',
                              Listen    => 5,
                              Reuse     => 1,
                             );
                           );
$read_set = new IO::Select;
$read_set->add($socketread);
while (1) {


        ($new) = IO::Select->select($read_set, undef, undef, undef);
        foreach $sock (@$new) {

                if ($sock == $socketread) {

                        while ($new_sock=$sock->accept()) {

                                while (defined ($buf=<$new_sock>)) {

                                        #receive and control $buf
                                        print "RECEIVE COMMAND:  ". $buf;
                                        $sock1 = new IO::Socket::INET (PeerAddr => 'server45',
                                                                      PeerPort => 1200,
                                                                      Proto    => 'tcp'
                                                                        );
                                        die "Socket could not be created. Reason: $! " unless $sock1;

                                        $aa=`$buf`;
                                        print $sock1 "$aa ";
                                        $sock1->flush();
                                        }

                                }
                }

        }
}

##################################################################################

#!/usr/bin/perl -w
#zclient2200.pl

use IO::Socket;
$sock = new IO::Socket::INET (PeerAddr => 'server1',
                              PeerPort => 2200,
                              Proto    => 'tcp'
);
die "Socket could not be created. Reason: $! " unless $sock;

####send data to srv
while () {


print "[COMMAND]# ";
$_=<STDIN>;
chomp($_);

print $sock "$_ ";
$sock->flush();
print "OK! ";
}

转载地址:http://cnhvi.baihongyu.com/

你可能感兴趣的文章
一起来看看protobuf中容易引起bug的一个细节
查看>>
无protobuf协议情况下的反序列化------貌似无解, 其实有解!
查看>>
make -n(仅列出命令, 但不会执行)用于调试makefile
查看>>
make -k(keep going)命令会在发现错误时继续执行(用于一次发现所有错误)
查看>>
makefile中“-“符号的使用
查看>>
一个奇葩的编译错误和解决方案------error: expected identifier before numeric constant
查看>>
又一次遇到undefined reference to xxx
查看>>
再遇"\xff\xfe"
查看>>
共享内存linux C/C++代码实战------顺便玩下ipcs, ipcrm, shmget, shmat, shmdt, shmctl
查看>>
为什么你的共享内存key为0且无法删除?
查看>>
玩转消息队列之C/C++代码
查看>>
linux中的ethtool命令
查看>>
迟延分段与分片: Segmentation and Checksum Offloading: Turning Off with ethtool (好文)
查看>>
一同事出现了http 404错误
查看>>
一同事出现了http 500错误
查看>>
最近遇到了http 400错误
查看>>
又是权限的问题啊啊啊!------远程定位一个与我不相关的问题
查看>>
udp引发的一起血案------message too long
查看>>
require_once失败------又是软链接惹的祸
查看>>
while (1) 引发的血案
查看>>