I just type after <Network programming whit perl>but it doesn't work like
the book tell me.

this is the two file, and I run the server.pl in my server and run
client.plwith this command:
client.pl localhost:2007
when I run the client.pl, the server.pl can return a message to tell me
there is a client connected. But, I type anything in client.pl and the
server.pl will do nothing. So I just add some print for debug, and I can't
found my debug message out, so the client.pl just stopped at "print $socket
$msg_out", when I use the ctrl+c to quit (p.s. I use archlinux), the server
will show the debug message, why?
Thanks for your reading, I'm Chinese and my English is poor, sorry.
------madper

------------------------------
Code
------------------------------

#!/usr/bin/perl -w
# server.pl --- alpha
# Author: madper <madper@myhost>
# Created: 08 Oct 2011
# Version: 0.01

use warnings;
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,
                                    Timeout     => 60*60,
                                    Reuse       => 1)
    or die "Can't create listening socket: $!\n";

warn "waiting for incoming connections on port $port...\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]\t";
    $session->autoflush(1);

    while (<$session>) {
        chomp;
        print $_;               # for debug
        $bytes_in += length($_);
        #chomp;
        my $msg_out = (scalar reverse $_) . CRLF;
        print $msg_out;         # for debug

        print $session $msg_out;
#        $session->print ("$msgout\n");

        $bytes_out += length ($_);
    }
    warn "Connection from [$peer, $port] finished\n";
    close $session;
}
print STDERR "bytes_sent = $bytes_out, bytes_received = $bytes_in\n";
close $sock;

------------------------------
------------------------------
Code
------------------------------

#!/usr/bin/perl -w
# client.pl --- alpha
# Author: madper <madper@myhost>
# Created: 08 Oct 2011
# Version: 0.01

use warnings;
use strict;
use IO::Socket;

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

my $host = shift || 'localhost';
my $port = shift || 'echo';
print "$host:port\n\n";

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

print $socket "asdf\n";
#my $msg_in = <&socket>;
#print STDOUT $msg_in;

#while (<STDIN>) {

while (defined (my $msg_out = STDIN->getline)) {
    print $msg_out . "this line is used for debug \n\n";
    print $socket $msg_out;
    print 'OK!!!\n';            # I can't get this line
                                # in my screen

    my $msg_in = <$socket>;
    print 'okok!!\n';

    print $msg_in;

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

$socket->close or warn $@;
print STDERR "bytes_sent = $bytes_out, bytes_received = $bytes_in\n";

------------------------------


-- 
-----------------========================-------------------------------
-----------------=====qq:303900672=========-------------------------------
-----------------=====电话:13580557950=====-------------------------------
-----------------=====中山大学 软件学院======-------------------------------
-----------------=====谢成骏==============--------------------------------

Reply via email to