Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

bind-address mysql multiple, sanibleone xxxx, ftp://192.168.100.100/, www.xxxcon, which comes first ob_start or session, wwwxxx/58/2010, xxxxdup, xxxxdup, mailx informatii, should producers of software-based services, such as atms, be held liable for economic injuries suffered when their systems fail?

Links

XODOX
Impressum

#1: perl question with socket

Posted on 2011-10-11 02:55:58 by bbboson

--20cf307c9ad4e23dd604aefb5ef6
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

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 =3D> 2007;
$/ =3D CRLF;
my ($bytes_out, $bytes_in) =3D (0, 0);

my $quit =3D 0;
#$SIG{INT} =3D sub { $quit++ };

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

warn "waiting for incoming connections on port $port...\n";
while (!$quit) {
next unless my $session =3D $sock->accept;

my $peer =3D gethostbyaddr ($session->peeraddr, AF_INET)
|| $session->peerhost;
my $port =3D $session->peerport;
warn "Connection from [$peer, $port]\t";
$session->autoflush(1);

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

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

$bytes_out +=3D length ($_);
}
warn "Connection from [$peer, $port] finished\n";
close $session;
}
print STDERR "bytes_sent =3D $bytes_out, bytes_received =3D $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 ) =3D (0, 0);

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

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

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

#while (<STDIN>) {

while (defined (my $msg_out =3D 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 =3D <$socket>;
print 'okok!!\n';

print $msg_in;

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

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

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


--=20
-----------------=============== 3D=====
=====3D-------------------------------
-----------------=====3Dqq:303900672=====3D= 3D===3D----=
---------------------------
-----------------=====çµè¯Âï¼Â1 3580557950=3D=
====-------------------------------
-----------------=====中山大= E5­¦ è½=
¯ä»¶å­¦é¢======------------ ---------------=
----
-----------------=====è°¢æÂÂéªÂ= 3D=====
=========3D--------------------------------

--20cf307c9ad4e23dd604aefb5ef6--

Report this message