R Blakers writes:
 > Hello,
 > 
 > We have hit what looks like a bug in the activestate (build 623)
 > implementation of the windows socket IO.  
 > 
 > What we are doing is writing a server will accept incoming requests on a
 > port and spawn in the same thread a client socket to handle the request.
 > The client socket returns an appropriate data buffer then closes down.
 > 
 > The problem is that after about 40 client sockets have been created and
 > shut down an error message about "unable to print on closed file handle" is
 > generated.  It appears that for each client/server request at least one
 > file handle is not being closed, and as a result windows eventually reaches
 > a limit on open file handles and the process dies.
 > 
 > We have written a simple test script below that simulates the problem and
 > generates this error.  Has anyone had any experience of this problem
 > before, or any suggestions on a work around??
 > 
 > Thanks in advance
 > Richard Blakers
 > 
 > //---------------code follows---------------------------------
 > #!/usr/bin/perl -w
 > 
 > use strict;
 > use IO::File;
 > use IO::Handle;
 > use Socket;
 > 
 > my $i;
 > my @fh=();
 > 
 > socket(Server, PF_INET, SOCK_STREAM, getprotobyname('tcp'))
 >     or die("socket [$!].\n");
 > setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l",1))
 >     or die("setsockopt [$!].\n");
 > bind(Server, sockaddr_in(1234, INADDR_ANY)) or die("bind [$!].\n");
 > listen(Server, SOMAXCONN);
 > 
 > my $client=new IO::Handle;
 > defined($client) or die("new IO::Handle [$!].\n");
 > 
 > # loop creating clients to handle a request - fails after about 40 iterations
 > for ($i=0; $i<1000000; $i++)
 > {
 >     my $f=accept($client,Server);
 >     unless (defined($client) and ref($client) =~ "IO::")
 >     {
 >      print "opened [$i] then fail because [$!] or [$^E].\n";
 >      defined($client) and print("client=",ref($client),"\n");
 >      exit 0;
 >     }
 >     print $client "Hello [$i].\n";
 >     flush $client;
 >     shutdown($client,2);
 >     defined($client) and $client->close();
 > #    push(@fh,$f);
 > }

I tried your code without any problem (at least up to a couple of
thousand connections). Perhaps its something to do with your setup
rather than Perl.

Also why not use IO::Socket::INET, it would make your code
simpler. For example:

#!/usr/bin/perl -w

use strict;
use IO::Socket::INET;

my $s = new IO::Socket::INET(Listen => 5, LocalAddr => 'localhost',
                             LocalPort => 1234, Proto => 'tcp');

for (my $i=0; $i<1000000; $i++)
{
    my $fh = $s->accept;
    defined $fh or die "Failed to accept connection $i: $! ($^E)\n";
    print $fh "Hello [$i].\n";
    $fh->close;
}

does pretty much what your sample code does.

HTH

-- 
Brian Raven
/* This bit of chicanery makes a unary function followed by
a parenthesis into a function with one argument, highest precedence. */
             -- Larry Wall in toke.c from the perl source code
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activeperl

Reply via email to