smackdab has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to create a non-blocking client / server app, using INET sockets. I am on ActiveState Perl on Win32

I have read the entire (every page ;-) of Network Programming With Perl (great book, but I am still stuck)

My program leaks 4K chunks of something quite regularly (by looking at taskmanager.)

Anyone mind running these 2 programs and see if it also leaks for you? Of course if you spot the bug that would be GREAT !!!

All error checking has been removed for readability

CLIENT ------
use IO::Socket; use IO::Select; use Time::HiRes; use strict; for (1..500) { my $socket = IO::Socket::INET->new("localhost:12002") or die "no s +ocket"; print $socket "this is a test 123\n"; print $socket "this is a test 456\n"; Time::HiRes::sleep(.5); $socket->close() or die "error closing"; }
SERVER ------
use IO::Socket; use IO::Select; use Time::HiRes; use strict; my $listen_socket; my $readers; my %sessions; CreateListener(); while (1) { Poll(); print "."; Time::HiRes::sleep(.08); } sub CreateListener() { $listen_socket = IO::Socket::INET->new(LocalPort => 12002, Proto => 'tcp', Reuse => 1, Listen => 50); $listen_socket->blocking(0); $readers = IO::Select->new($listen_socket); $readers->add($listen_socket); } sub Poll { my @ready = $readers->can_read(0); return if (!@ready); my $handle = shift @ready; if ($handle) { if ($handle eq $listen_socket) { my $connect = $listen_socket->accept(); $sessions{"$connect"}{status} = 'first_connect'; $readers->add($connect); } else { my $user_input; my $bytes = sysread($handle, $user_input, 16384); if ($bytes && $bytes > 0) { chomp($user_input); if ($sessions{"$handle"}{status} eq "first_connect") { # Save data until socket is closed... $sessions{"$handle"}{status} = "next_connect"; $sessions{"$handle"}{data} = $user_input; } else { # Append data until socket is closed... $sessions{"$handle"}{data} .= $user_input; } } if (!$bytes) { # Got it all print $sessions{"$handle"}{data} . "\n"; # Close this socket $readers->remove($handle); delete $sessions{"$handle"}{status}; delete $sessions{"$handle"}{data}; close $handle; undef $handle; } } } }

Replies are listed 'Best First'.
Re: nonblocking but leaking...
by Fastolfe (Vicar) on Mar 30, 2002 at 03:23 UTC
    It's not obvious to me what's up (though somebody else may catch something), but if I had this problem, I might put some debugging code after each client request that does a dump of all of the variables in my code (with Devel::Symdump and/or Data::Dumper or some such). Is it possible that %sessions is growing? If you've ruled out all of your own variables, there's probably something more sinister going on...
      Devel::Symdump didn't do much for me, but Data::Dumper is pretty cool. It pointed out that %sessions was growing, and I would have bet a million that it was fixed...unfortunately not ;-(

      Thanks for suggesting it ! It gives me an area to explore as I was stuck w/o any idea of how to proceed...