Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: TCP Server hangs with multiple client connections

by Khen1950fx (Canon)
on Aug 02, 2012 at 19:45 UTC ( #985097=note: print w/ replies, xml ) Need Help??


in reply to TCP Server hangs with multiple client connections

I looked at your server socket. There's a problem with IO::Select's can_read. If there's no timeout given, or if it contains a registered handler, can_read will block. Here's how I would handle the socket:

#!/usr/bin/perl select(STDERR); $| = 1; select(STDOUT); $| = 1; use autodie; use strictures 1; use IO::Socket; use IO::Select; use Log::Log4perl; my $sock = new IO::Socket::INET( LocalAddr => 'www.example.com', LocalPort => 9000, Listen => 128, Reuse => 1, Blocking => 0, Proto => 'tcp', ); our $selSock = new IO::Select($sock); while( my(@ready) = $selSock->can_read(10) ) { use strict qw/refs/; use warnings FATAL => 'syntax'; foreach $_ (@ready) { if ($_ == $sock) { my $logger; my $clientPool; $logger->info("Base socket is " . $sock); $logger->info("Adding new socket"); my $newSock = $sock->accept or die; $logger->info("Added new socket " . $newSock); $selSock->add($newSock); my $fd = $selSock->fileno($newSock); my $address = inet_ntoa($newSock->peeraddr); my @values = ($fd, $address); if ($clientPool->queueJob( queueValues => \@values)) { $logger->error( "Failed to queue client connection" ); my $trash = <$newSock>; $trash->send($newSock, "** ENQUEUE_FAILED **", 0); $trash->send($newSock, "** ZERO_BYTES_LEFT **", 0); $selSock->remove($trash); $newSock = undef; sleep 1; exit; } } } }


Comment on Re: TCP Server hangs with multiple client connections
Download Code
Re^2: TCP Server hangs with multiple client connections
by rmahin (Beadle) on Aug 02, 2012 at 20:53 UTC
    Tried what you suggested, changed it a little though to
    while(1) { use strict qw/refs/; use warnings FATAL => 'syntax'; my @ready = $selSock->can_read(10); unless (@ready) {next};
    so that it wouldn't die if no clients connected. But anyway, and had no effect that I could see. Thanks for the suggestion though!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985097]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2015-07-07 11:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (88 votes), past polls