方法一: **************************************************************client端 #!/usr/bin/perl use strict; use IO::Socket; use Data::Dumper; my $lsocket=IO::Socket::INET->new( PeerAddr=>'127.0.0.1',PeerPort=>'4321',Proto=>'tcp',); print $lsocket "ls /home/ywang/;echo test;ifconfig"; $lsocket->shutdown(1); while(<$lsocket>){ print "$_"; #print Dumper $lsocket; } **************************************************************server端 #!/usr/bin/perl use strict; use IO::Socket; my $lsocket=IO::Socket::INET->new( LocalAddr=>'127.0.0.1',LocalPort=>'4321',Listen=>SOMAXCONN,Reuse=>1,Timeout=>30,); #=cut while(1){ my $tmpsocket = $lsocket->accept; next unless defined($tmpsocket); while(<$tmpsocket>){ my $cmd_from_client=$_; print "cmd from client is: $cmd_from_client\n"; my $rr=`$cmd_from_client` or die "Can not execute the cmd from client\n"; print $tmpsocket $rr; } $tmpsocket->shutdown(1); print "end print\n"; } 方法二 client端 #!/usr/bin/perl use strict; use IO::Socket; use Data::Dumper; my $lsocket=IO::Socket::INET->new( PeerAddr=>'127.0.0.1',); my $byte = $lsocket->send('this is test'); print "Send $byte\n"; my $fromserver; $lsocket->recv($fromserver,30); print "get infor from server $fromserver\n"; sleep 2; $byte=$lsocket->send('test'); $lsocket->recv($fromserver,30); print "2nd get infor $fromserver\n"; server端 #!/usr/bin/perl use strict; use IO::Socket; my $lsocket=IO::Socket::INET->new( LocalAddr=>'127.0.0.1',); my $tmpsocket = $lsocket->accept; next unless defined($tmpsocket); my $getcmd; $tmpsocket->recv($getcmd,12); print "get your cmd $getcmd\n"; $tmpsocket->send('Server get your cmd'); $tmpsocket->recv($getcmd,10); print "2nd get your cmd $getcmd\n"; $tmpsocket->send('2nd Server get your cmd');
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。