为什么不行?:

为什么不行?:

为什么不行?:
1 这是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";
请帮忙!!!

晕了,我对网络...
晕了,我对网络编程不熟悉,支持你一下。
你这个是TCP程序吧?

我对TCP不是很熟悉哦。

我们一般在UDP里发送数据使用send方法,要指定对方的IP和端口才发过去。
把客户端--wh...
把客户端
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);
}
改成
while (my $msg_out = <>) {
$msg_out .= "\r\n";
send(SOCK, $msg_out,0) or die "send failed: $!\n";
my $msg_in = '';
recv(SOCK, $msg_in, 1024,0) or die "recv failed: $!\n";
print $msg_in;
$bytes_out+=length($msg_out);
$bytes_in +=length($msg_in);
}