Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

Wwwxxxapc, CAM::PDF::Renderer::TextF Fragen, adodb.recordset error '800a0cc1', php save mails as eml, WWWXXXAPC, sqlexpress database file auto-creation error, sqlexpress database file auto-creation error, dbf2mysql parameter, wwwxxxAPC, How to unsubscrube from dategen spam



#1: perl question with socket

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

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 in my server and run
client.plwith 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.


#!/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 =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";

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


#!/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 ) =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 $@;

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";


-----------------=============== 3D=====
-----------------=====3Dqq:303900672=====3D= 3D===3D----=
-----------------=====电话:1 3580557950=3D=
-----------------=====中山大= E5­¦ è½=
¯ä»¶å­¦é™¢======------------ ---------------=
-----------------=====谢成骏= 3D=====


Report this message