while (1) in server

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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.

Re: while (1) in server

Quoted text here. Click to load it

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

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

Quoted text here. Click to load it

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

Quoted text here. Click to load it

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).

Quoted text here. Click to load it

That's a bit difficult since it's not clear to me what exactly you
want to achieve.
                              Regards, Jens
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

Re: while (1) in server

On 03/05/2010 20:44, Taras Kudla wrote:
Quoted text here. Click to load it



Site Timeline