CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
simple network program question, thx!~

 



madper
New User

Oct 9, 2011, 6:19 AM

Post #1 of 1 (580 views)
simple network program question, thx!~ Can't Post

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.pl with 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";



(This post was edited by madper on Oct 9, 2011, 6:21 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives