另外一个网络的编程问题

另外一个网络的编程问题

另外一个网络的编程问题
我在看<perl网络编程》这本书是有下面一个例子。
分别是第四章和第五章的例子。
显示使用模块Socket

客户端 (我们称之为客户端1 ):
#!/usr/bin/perl -w
use strict;
use Socket;
use IO::Handle;

my ($bytes_out, $bytes_in) = (0, 0);
my $host = shift || 'localhost';
my $port = shift || getservbyname('echo', 'tcp');

my $protocol = getprotobyname('tcp');

$host = inet_aton($host);
my $data_addr = sockaddr_in($port, $host);

socket(SOCK, AF_INET, SOCK_STREAM, $protocol) or die "$!\n";
# connect the server
connect(SOCK, $data_addr) or die "Can't Connect the Server: $!\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 "bytes send = $bytes_out, bytes received = $bytes_in\n";


再是服务器端 ( 我们称之为服务端1 ):
#!/usr/bin/perl
use strict;
use warnings;
use Socket;
use IO::Handle;
use constant MY_ECHO_PORT => 2007;

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

my $port = shift || MY_ECHO_PORT;
my $protocol = getprotobyname('tcp');

$SIG{INT} = sub {
print STDERR "bytes_send = $bytes_out, bytes_received = $bytes_in\n";
exit 0;
};

socket(SOCK, AF_INET, SOCK_STREAM, $protocol) or die "Socket failed: $!";
setsockopt(SOCK, SOL_SOCKET, SO_REUSEADDR, 1) or die "$!\n";

my $my_addr = sockaddr_in($port, INADDR_ANY);

bind(SOCK, $my_addr);
listen(SOCK, SOMAXCONN) or die "Listen() failed:$!\n";

warn "Waitng for incoming connection on port $port...\n";

while (1) {
next unless my $remote_addr = accept(SESSION, SOCK);
my ($port, $hisaddr ) = sockaddr_in($remote_addr);

warn "Connection from [", inet_ntoa($hisaddr), ", $port]\n";

SESSION->autoflush(1);

while ( <SESSION> ) {
$bytes_in += length($_);
chomp;

my $msg_out = (scalar reverse $_). "\n";
print SESSION $msg_out;
$bytes_out += length($msg_out);
}

warn "Connection from [", inet_ntoa($hisaddr), ", $port]\n";
close SESSION;
}

close SOCK;


然后再使用IO::Socket模块重写:

客户端 ( 我们称之为客户端2):
#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket;

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

my $host = shift || 'localhost';
my $port = shift || 'echo';

$SIG{PIPE} = sub {
print "it had a PIPE error.\n";
exit 0;
};

my $socket = IO::Socket::INET->new("$host:$port") or die "$@\n";

while ( defined( my $msg_out = STDIN->getline ) ) {
print $socket $msg_out;
my $msg_in = <$socket>;

print $msg_in;

$bytes_out += length $msg_out;
$bytes_in += length $msg_in;
}

$socket->close;
print STDERR "bytes send = $bytes_out, bytes_received = $bytes_in\n";

服务器端(我们称之为:服务端2):
#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket qw(:DEFAULT :crlf);
use constant MY_ECHO_PORT => 2007;

$/ = CRLF;

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

my $quit = 0;

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

my $port = shift || MY_ECHO_PORT;

my $socket = IO::Socket::INET->new(
Listen => 20,
LocalPort => $port,
Timeout => 60 * 60,
Reuse => 1
) or die "Can't create listen port: $!\n";

warn "Waiting for incoming connections on port $port ......\n";

while ( !$quit ) {
next unless my $session = $socket->accept;

$session->autoflush(1);

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

$session->peerhost;

my $port = $session->peerport;

warn "Connection from [$peer, $port]\n";

while ( my $str = <$session> ) {
print $str, "\n";
$bytes_in += length $str;
chomp $str;
my $msg_out = (scalar reverse $str)."\n";
print $msg_out, "\n";
print $session $msg_out;

$bytes_out += length $msg_out;
}

warn "Connection from [$peer, $port] finished\n";
close $session;
}

print STDERR "bytes send = $bytes_out, bytes received = $bytes_in\n";

close $socket;


现在问题就来了。
如果我们使用客户端1和客户端2连接服务器端1, 没有问题,很正常。
但是如果我使用客户端1和客户端2连接服务器端2是就有问题,
发现客户端接受不到任何返回。

比较奇怪,请帮我看看。
谢谢。
自己顶起来。--.
自己顶起来。
再顶一下。--.
再顶一下。
[quote]--my $peer = [c.
[quote]
my $peer = [color=red]gethostbyaddr[/color]( $session->peeraddr, AF_INET) ||
[/quote]

大概是执行gethostbyaddr()的时候一直在反查域名,所以停在那里了

http://www.opengroup.org/onlinepubs/009695399/functions/gethostbyaddr.html
删掉在服务器2中的代码:<.
删掉在服务器2中的代码:
$/ = CRLF;

直接使用默认分隔符"\n"就可以了。