为什么不行?
这是perl 网络编程中的一个例子:
#!/usr/bin/perl
use strict;
use IO::Socket qw(:DEFAULT :crlf);
use constant MY_ECHO_PORT=>2007;
$/=CRLF;
my ($bytes_out,$bytes_in)=(0,0);
my $port =shift ||MY_ECHO_PORT;
my $quit=0;
$SIG{'INT'}=sub { $quit++ };
my $sock=IO::Socket::INET->new( Listen =>20,
LocalPort =>$port,
Timeout =>60*60,
Reuse =>1 ) or die "Can not creat listening socket:$!\n";
while(!$quit) {
next unless my $session=$sock->accept;
my $peer=gethostbyaddr( $session->peeraddr,AF_INET) || $session->peerhost;
my $port=$session->peerport;
warn "Connection from [$peer,$port]\n";
while (<$session>) {
$bytes_in +=length($_);
chomp;
my $msg_out=(scalar reverse $_) . CRLF;
print $session $msg_out;
#print $msg_out;
$bytes_out+=length($msg_out);
}
warn "Connection from [$peer,$port] finished\n";
close $session;
}
print STDERR "bytes_sent =$bytes_out,bytes_recieved= $bytes_in\n";
close $sock;
效果是把客户端的输入反序后再发给客户端,但在我这里什么也不作,这是为什么,客户程序
use strict;
use Socket;
use IO::Handle;
use constant MY_PORT=>2007;
my ($bytes_out,$bytes_in)=(0,0);
my $host=shift||'localhost';
my $port=shift|| MY_PORT;
print $port."\n";
my $protocol=getprotobyname('tcp');
$host=inet_aton($host) or die "$host:unknown host";
socket (SOCK,AF_INET,SOCK_STREAM,$protocol) or die "socket() failed:$!";
my $dest_addr=sockaddr_in($port,$host);
connect(SOCK,$dest_addr) or die "connect failed:$!\n";
SOCK->autoflush(1);
while (my $msg_out=<>) {
print SOCK $msg_out;
my $msg_in=<SOCK>;
print $msg_in;
$bytes_out+=length($msg_out);
$bytes_in +=length($msg_in);
}
close SOCK;
print STDEER "bytes_sent=$bytes_out,bytes_recieved=$bytes_in\n";
请帮忙!!!