天天看點

[Perl]測試程式時用Perl真是太友善了

測試自己的業務資料遠端傳送服務,需要一個能模拟對方Socket的服務,

這時候就顯出Perl的便利了。一分鐘搞定。

代碼如下,其實也是從CPAN那裡拿來的:

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

#

# 工程項目: 模拟Socket伺服器

#

# 子產品任務: 來自intenel,這個腳本提供一個TCP echo 服務,版本是2

#

# 程式名稱:  TcpEchoServerForLDB.pl

#

# 程式說明: 

#            監聽固定Socket端口,如果收到資料,就給對方傳回一個字元串“ok回車”,

#    這樣來模拟接收資料的Socket服務。

#

# 使用說明:TcpEchoServerForLDB.pl [port]

#           [port]指要監聽的端口号

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

#!/usr/bin/perl

# Figure 5.4: The reverse echo server, using IO::Socket

##================================================================##

system("cls");

warn("/n");

warn("TCP ECHO Simple Server Version 2.0/n");

warn("沒有版權 Linktone 2005-2006。不保留所有權利。/n");

warn("/n");

warn("用法: TcpEchoServerForLDB.pl [port]/n");

warn("[注意]如果沒有傳遞端口參數,預設監聽4330端口/n");

warn("[注意]發送的資料請以回車換行結尾!/n");

##================================================================##

##================================================================##

## 引用的庫聲明 2

use strict;

use IO::Socket qw(:DEFAULT :crlf);

##================================================================##

##================================================================##

use constant MY_ECHO_PORT => 4330;

$/ = CRLF;

my ($bytes_out,$bytes_in) = (0,0);

my $quit = 0;

$SIG{INT} = sub { $quit++ };

my $port = shift || MY_ECHO_PORT;

my $sock = IO::Socket::INET->new( Listen => 20,

LocalPort => $port,

Timeout => 60*60,

Reuse => 1)

or die "Can't create listening socket: $!/n";

warn "服務守候在端口$port.../n";

while (!$quit)

{

 next unless my $session = $sock->accept;

 my $peer = gethostbyaddr($session->peeraddr,AF_INET) || $session->peerhost;

 my $port = $session->peerport;

 warn "她來了:[$peer,$port]/n";

 # 要回送的字元串

 my $msg_Output = "ok回車";

 while (<$session>)

 {

  $bytes_in += length($_);

  chomp;

  my $msg_out = (scalar reverse $_) . CRLF;

  warn("Message From [$peer,$port]:$msg_out");

  print $session $msg_Output;

  ##warn("Send Message To [$peer,$port]:$msg_Output");

  $bytes_out += length($msg_Output);

 }

 warn "她走了[$peer,$port]!/n";

 close $session;

}

##================================================================##

print STDERR "bytes_sent = $bytes_out, bytes_received = $bytes_in/n";

warn("bytes_sent = $bytes_out, bytes_received = $bytes_in/n");

close $sock;

測試自己的業務資料遠端傳送服務,需要一個能模拟對方Socket的服務,

這時候就顯出Perl的便利了。一分鐘搞定。

代碼如下,其實也是從CPAN那裡拿來的:

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

#

# 工程項目: 模拟Socket伺服器

#

# 子產品任務: 來自intenel,這個腳本提供一個TCP echo 服務,版本是2

#

# 程式名稱:  TcpEchoServerForLDB.pl

#

# 程式說明: 

#            監聽固定Socket端口,如果收到資料,就給對方傳回一個字元串“ok回車”,

#    這樣來模拟接收資料的Socket服務。

#

# 使用說明:TcpEchoServerForLDB.pl [port]

#           [port]指要監聽的端口号

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

#!/usr/bin/perl

# Figure 5.4: The reverse echo server, using IO::Socket

##================================================================##

system("cls");

warn("/n");

warn("TCP ECHO Simple Server Version 2.0/n");

warn("沒有版權 Linktone 2005-2006。不保留所有權利。/n");

warn("/n");

warn("用法: TcpEchoServerForLDB.pl [port]/n");

warn("[注意]如果沒有傳遞端口參數,預設監聽4330端口/n");

warn("[注意]發送的資料請以回車換行結尾!/n");

##================================================================##

##================================================================##

## 引用的庫聲明 2

use strict;

use IO::Socket qw(:DEFAULT :crlf);

##================================================================##

##================================================================##

use constant MY_ECHO_PORT => 4330;

$/ = CRLF;

my ($bytes_out,$bytes_in) = (0,0);

my $quit = 0;

$SIG{INT} = sub { $quit++ };

my $port = shift || MY_ECHO_PORT;

my $sock = IO::Socket::INET->new( Listen => 20,

LocalPort => $port,

Timeout => 60*60,

Reuse => 1)

or die "Can't create listening socket: $!/n";

warn "服務守候在端口$port.../n";

while (!$quit)

{

 next unless my $session = $sock->accept;

 my $peer = gethostbyaddr($session->peeraddr,AF_INET) || $session->peerhost;

 my $port = $session->peerport;

 warn "她來了:[$peer,$port]/n";

 # 要回送的字元串

 my $msg_Output = "ok回車";

 while (<$session>)

 {

  $bytes_in += length($_);

  chomp;

  my $msg_out = (scalar reverse $_) . CRLF;

  warn("Message From [$peer,$port]:$msg_out");

  print $session $msg_Output;

  ##warn("Send Message To [$peer,$port]:$msg_Output");

  $bytes_out += length($msg_Output);

 }

 warn "她走了[$peer,$port]!/n";

 close $session;

}

##================================================================##

print STDERR "bytes_sent = $bytes_out, bytes_received = $bytes_in/n";

warn("bytes_sent = $bytes_out, bytes_received = $bytes_in/n");

close $sock;