From: Taras Kudla on
hi to everybody,
i am new in perl and writing a simple server, and i have a problem,
for handling client connections i am using a while script, processor
usage during script running is 100%.
here is piece of code:
.....
my $socket=IO::Socket::INET->new(LocalPort =>
$port,Listen=>SOMAXCONN,Reuse=>1) or die "Cannot create listen socket:
$!";
......
sub handle_connection {
my $socket=shift;
....
while(1) {
my $answ=<$socket>;
}
}
can anyone suggest some other methods of connection handling or some
piece of code maybe.
thanks
From: Jens Thoms Toerring on
Taras Kudla <taraskudla(a)gmail.com> wrote:
> hi to everybody,
> i am new in perl and writing a simple server, and i have a problem,
> for handling client connections i am using a while script, processor
> usage during script running is 100%.
> here is piece of code:
> ....
> my $socket=IO::Socket::INET->new(LocalPort =>
> $port,Listen=>SOMAXCONN,Reuse=>1) or die "Cannot create listen socket:
> $!";
> .....

I assume you have lines here left out with at least something like

$client = $socket->accept();
handle_connection( $client );

> sub handle_connection {
> my $socket=shift;
> ....
> while(1) {
> my $answ=<$socket>;
> }
> }

What you do here (at least as posted) is looping without taking
into account that the other side may already have closed the
connection. In that case the line

> my $answ=<$socket>;

will return immediately with $answ being undefined. But you continue
to loop regardless, thus sending your program into an infinite loop
that will eat up all CPU time it can get. This could be avoided
by e.g. using instead

while ( my $answ = <$socket> ) {
....
}

This way the loop will terminate once $answ is undiefined (i.e. when
the other side has closed the connection).

> can anyone suggest some other methods of connection handling or some
> piece of code maybe.

That's a bit difficult since it's not clear to me what exactly you
want to achieve.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt(a)toerring.de
\__________________________ http://toerring.de
From: RedGrittyBrick on
On 03/05/2010 20:44, Taras Kudla wrote:
> hi to everybody,
> i am new in perl and writing a simple server, and i have a problem,
> for handling client connections i am using a while script, processor
> usage during script running is 100%.
> here is piece of code:
> ....
> my $socket=IO::Socket::INET->new(LocalPort =>
> $port,Listen=>SOMAXCONN,Reuse=>1) or die "Cannot create listen socket:
> $!";
> .....
> sub handle_connection {
> my $socket=shift;
> ....
> while(1) {
> my $answ=<$socket>;
> }
> }
> can anyone suggest some other methods of connection handling or some
> piece of code maybe.
> thanks

http://perldoc.perl.org/perlipc.html#Sockets%3a-Client%2fServer-Communication


--
RGB