关于Perl调用Socket模块进行C/S通信的问题求解

关于Perl调用Socket模块进行C/S通信的问题求解

关于Perl调用Socket模块进行C/S通信的问题求解
我参照CPAN和perldoc的文档写了下面的代码,目的是实现从客户端向服务器发任意字符串,服务器接收到以后立刻回显到客户端,但是不知道为什么总是失败,发送的消息服务器收不到。困扰了好几天了,第一次用Perl,没有什么经验,perlfunc的文档看了无数遍还是云里雾里,代码如下,请高人指点:
客户端代码:
1 #!/usr/bin/env perl
2 use strict;
3 use Socket;
4 my $EOL="\015\012";
5 my ($remote,$port,$iaddr,$paddr,$proto,$line);
6 $remote = shift || "localhost";
7 $port = shift || "3000";
8 if($port=~/\D/){$port=getservbyname($port,'tcp')};
9 die "没有可用的端口。" unless $port;
10 $iaddr = inet_aton($remote);
11 $paddr = sockaddr_in($port,$iaddr);
12 $proto = getprotobyname('tcp');
13 print "主机:$remote\n";
14 print "端口:$port\n";
15 print "协议:$proto\n";
16 socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "创建Socket失败: $!";
17 connect(SOCK,$paddr) || die "连接失败:$!"; #连接到socket
18 print "连接成功:$paddr,$remote,$iaddr\n";
19 my $kidpid;
20 # split the program into two processes, identical twins
21 die "can't fork: $!" unless defined($kidpid = fork());
22 # the if{} block runs only in the parent process
23 if ($kidpid) {
24 # copy the socket to standard output
25 while (defined ($line = <SOCK>)) {
26 print STDOUT $line;
27 }
28 kill("TERM", $kidpid); # send SIGTERM to child
29 }
30 # the else{} block runs only in the child process
31 else {
32 # copy standard input to the socket
33 while (defined ($line = <STDIN>)) {
34 print SOCK $line,$EOL;
35 };
36 };
37 close (SOCK) || die "close: $!";
38 exit;
服务端代码:
1 #!/usr/bin/env perl
2 use strict;
3 BEGIN { $ENV{PATH} = '/usr/ucb:/bin' };
4 use Socket;
5 use Carp; # warn of errors (from perspective of caller)
6 my $EOL = "\015\012";
7 sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" };
8 my $port = shift || 3000;
9 my $proto = getprotobyname('tcp');
10 ($port) = $port =~ /^(\d+)$/ or die "无效的端口:$port";
11 socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
12 setsockopt(Server, SOL_SOCKET, SO_REUSEADDR,pack("l", 1));
13 bind(Server, sockaddr_in($port, INADDR_ANY)) || die "bind: $!";
14 listen(Server,SOMAXCONN) || die "listen: $!";
15 logmsg "服务启动,端口: $port";
16 my $paddr;
17 $SIG{CHLD} = \&REAPER;
18 $SIG{INT} = sub {die "服务被中断:$!\n"};
19 for ( ; $paddr = accept(Client,Server);close Client){
20 my($port,$iaddr) = sockaddr_in($paddr);
21 my $name = gethostbyaddr($iaddr,AF_INET);
22 logmsg "连接来自: $name [",inet_ntoa($iaddr), "] 端口是: $port";
23 print Client "欢迎你! $name, 现在是 ",scalar localtime,"\n";
24 my $line;
25 while($line=<Client>){
26 print Client $line,$EOL;
27 };
28 };




   

是哪本书的?.
不是书上的,是CPAN的例子.
不是书上的,是CPAN的例子,我稍微修改了一下,现在已经解决了。