+ -
当前位置:首页 → 问答吧 → perl 正则表达式问题

perl 正则表达式问题

时间:2010-10-09

来源:互联网

本帖最后由 mitmax 于 2010-10-09 11:02 编辑
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use IO::Socket;

  4. my $sock = new IO::Socket::INET(
  5.     LocalAddr => '192.168.1.240',
  6.     LocalPort => 7777,
  7.     Proto     => 'tcp',
  8.     Listen    => 5
  9. );
  10. die "Could not create socket: $!\n" unless $sock;

  11. my @result;
  12. my $i = 0;

  13. while (1) {
  14.     next unless my $client = $sock->accept();
  15.     while ( my $get = <$client> ) {

  16.         #print $get;
  17.         &Test_Sockte($get);
  18.     }
  19.     close $client;
  20. }

  21. sub Test_Sockte {
  22.     my ($getdata) = @_;
  23. #    print $getdata; 打印的结果是  ip:192.168.1.240:type:gateway:value:37
  24.     if ( my ( $host, $type, $value ) =
  25.         $getdata =~ /ip:(\d+\.\d+\.\d+\.\d+):type:(\w+):value:(\d+)/g )
  26.     {
  27.         print $host, $type;
  28.     }
  29.     else {

  30.         print "Error";
  31.     }
  32. }
复制代码


就算不匹配为什么连print "Error"; 也没执行.


  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;

  4. use IO::Socket;

  5. my $send_sock = new IO::Socket::INET(
  6.     PeerAddr => '192.168.1.240',
  7.     PeerPort => 7777,
  8.     Proto    => 'tcp'
  9. );
  10. die "$!" unless $send_sock;
  11. my $GerCon=`netstat -an|grep tcp|wc -l`;
  12. print $send_sock("ip:192.168.1.240:","type:gateway:","value:$GerCon:");
复制代码

作者: mitmax   发布时间: 2010-10-09

本帖最后由 珞水的大叔 于 2010-10-09 11:28 编辑

改为
  1.         print "$host, $type\n";
  2.     }
  3.     else {

  4.         print "Error\n";
复制代码
或者
在sub Test_Sockte里第一行加入
  1. $| = 1;
复制代码

作者: 珞水的大叔   发布时间: 2010-10-09