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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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


New User

Oct 9, 2011, 6:19 AM

Post #1 of 1 (706 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 in my server and run with this command: localhost:2007
when I run the, the can return a message to tell me there is a client connected. But, I type anything in and the will do nothing. So I just add some print for debug, and I can't found my debug message out, so the 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

#!/usr/bin/perl -w 
# --- 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";

while (<$session>) {
print $_; # for debug
$bytes_in += length($_);
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;

#!/usr/bin/perl -w 
# --- 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 $@;

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