×

Loading...
Ad by
  • 予人玫瑰,手有余香:加拿大新天地工作移民诚聘求职顾问&行业导师!
Ad by
  • 予人玫瑰,手有余香:加拿大新天地工作移民诚聘求职顾问&行业导师!

perl 高手请来看一下。

本文发表在 rolia.net 枫下论坛正在学perl, 一个tcp echo server, 一个tcp echo client 程序, 为什么client 程序socket总是连不上呢(本机)?
“C:\Perl\test>telnet 127.0.0.1 2007
Connecting To 127.0.0.1...Could not open connection to the host, on port 2007: C
onnect failed”
=================== server 源码 ==========================
#!/usr/bin/perl -w
#File: tcp_echo_server.pl
#usage: tcp_echo_server.pl [port]

use strict;
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 $sock = IO::Socket::INET->new( listen => 20,
LocalPort => $port,
LocalAddr => INADDR_ANY,
Timeout => 60*60,
Proto => 'tcp',
Reuse =>1)
or die "Can't creat listening socket: $!\n";

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


my $myaddr = inet_ntoa($sock->sockaddr());
my $myport = $sock->sockport();
print "local socket is: $myaddr : $myport\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;
$bytes_out += length($msg_out);
}
warn "Connection from [$peer, $port] finished\n";
close $session;
}

print STDERR "Bytes_Sent = $bytes_out, Bytes_Received = $bytes_in\n";

close $sock;

====================== client 源码 =================================
#!/usr/bin/perl -w
#File: tcp_echo.pl
#usage: tcp_echo.pl [host] [port]

use strict;
use IO::Socket;
my ($bytes_out, $bytes_in) = (0, 0);

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

my $sock = IO::Socket::INET->new( Proto => 'tcp',
PeerAddr => $host,
PeerPort => $port) or die "Can't connect : $!";


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

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

$sock->close or warn $!;

print STDERR "bytes_sent = $bytes_out, bytes_received = $bytes_in \n";更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / 专业知识杂谈 / perl 高手请来看一下。
    本文发表在 rolia.net 枫下论坛正在学perl, 一个tcp echo server, 一个tcp echo client 程序, 为什么client 程序socket总是连不上呢(本机)?
    “C:\Perl\test>telnet 127.0.0.1 2007
    Connecting To 127.0.0.1...Could not open connection to the host, on port 2007: C
    onnect failed”
    =================== server 源码 ==========================
    #!/usr/bin/perl -w
    #File: tcp_echo_server.pl
    #usage: tcp_echo_server.pl [port]

    use strict;
    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 $sock = IO::Socket::INET->new( listen => 20,
    LocalPort => $port,
    LocalAddr => INADDR_ANY,
    Timeout => 60*60,
    Proto => 'tcp',
    Reuse =>1)
    or die "Can't creat listening socket: $!\n";

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


    my $myaddr = inet_ntoa($sock->sockaddr());
    my $myport = $sock->sockport();
    print "local socket is: $myaddr : $myport\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;
    $bytes_out += length($msg_out);
    }
    warn "Connection from [$peer, $port] finished\n";
    close $session;
    }

    print STDERR "Bytes_Sent = $bytes_out, Bytes_Received = $bytes_in\n";

    close $sock;

    ====================== client 源码 =================================
    #!/usr/bin/perl -w
    #File: tcp_echo.pl
    #usage: tcp_echo.pl [host] [port]

    use strict;
    use IO::Socket;
    my ($bytes_out, $bytes_in) = (0, 0);

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

    my $sock = IO::Socket::INET->new( Proto => 'tcp',
    PeerAddr => $host,
    PeerPort => $port) or die "Can't connect : $!";


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

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

    $sock->close or warn $!;

    print STDERR "bytes_sent = $bytes_out, bytes_received = $bytes_in \n";更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • the "listen => 20 " should be upper case "L"... use netstat -an |grep LISTEN to check.....
      • thanks. another thing wrong is that SOCKET IO use CRLF (as defined in server side), while STANDARD IO use CR or LF. Now is working. Thanks for your help.