Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: TCP Server hangs with multiple client connections

by suaveant (Parson)
on Aug 02, 2012 at 18:20 UTC ( [id://985087]=note: print w/replies, xml ) Need Help??


in reply to TCP Server hangs with multiple client connections

I've seldom used threads, but here is a suggestion to simplify things. I see no reason to have a central worker thread in the scenario you are presenting. It looks like the only thing you are doing is handling multiple requests individually. Socket accept works in a round robin fashion that works well in a mutli thread/process environment. Create your socket and then create threads/forks, have all of them do a while($sock->accept()) and you are good to go, don't complicate things with queues and threading unless you really need it. Even if you keep the threading for some reason, I'd still suggest handling the socket this way. It'll simplify your code and probably clear up your issues which most likely are caused by some sort of unresolved race condition.

I'd look at Net::Server, not only will it hide a lot of code and prevent you from re-inventing the wheel, it even has a mechanism for handling a listen port and log4perl it seems. Might save you a lot of headaches.

Edit Net::Server also can do useful things like have a min/max connection pool that grows your processes dynamically

                - Ant
                - Some of my best work - (1 2 3)

  • Comment on Re: TCP Server hangs with multiple client connections

Replies are listed 'Best First'.
Re^2: TCP Server hangs with multiple client connections
by rmahin (Scribe) on Aug 02, 2012 at 19:05 UTC

    Thanks for the quick reply! Oh man that is an excellent idea. I wasn't aware that accept() worked like that, will try it and update with results.

    Will also definitely take a look at that module, Net::Server. We are hesitant to move to completely different way of handling everything as we've invested a lot of time on our current implementation, but if we cant work out this issue may have to...REALLY regretting not having found that module when we first started, at first glance it looks pretty nice.

      Well, I'd guess you'd be able to pretty much copy the code you use for processing directly into a Net::Server interface without a lot of changes, since you don't really seem to be using the threads for anything but parallel workers.

      Something along the lines of this, not tested, of course.

      sub process_request { my $self = shift; my $fh = $self->{server}->{client}; my $select = IO::Select->new(); $select->add($fh); my $last = 1; while ($last) { my $subroutineCall; my $command; my $completeLine = ""; # check if anything is ready to be received from client my @ready = $select->can_read(.1); next unless (@ready); # loop while receiving from the client untill a full msg has b +een received as identified by our flags while ( defined( my $line = <$fh> ) ) { if ( $line =~ /\*\*\* CLOSE_CONNECTION_CLIENT \*\*\*\n$/ ) + { $logger->info("Client closed connection to the server" +); $last = 0; $selSock->remove($fh); shutdown( $fh, 2 ); close $fh; last; } $completeLine .= $line; if ( $completeLine =~ /(.+) \*\*\* COMMAND_INPUT \*\*\* (.+) \*\*\* COMMA +ND_INPUT_END \*\*\*\n$/is ) { $subroutineCall = $1; $command = $2; $logger->info( "Processing message \"" . $command . "\ +"" ); last; } } if ($last) { #checking if the subroutine call is defined, if not, it kills that cli +ent connection (for cases of closing out of a client window and it se +nding the empty string ) if ( defined($subroutineCall) ) { $logger->debug("Calling subroutine '${subroutineCa +ll}'"); my ($string) = executeCommand( subCall => $subroutineCall, command => $command ); send( $fh, "$string\n*** ZERO_BYTES_LEFT ***", 0 ) +; } else { $logger->info( "Undefined subroutine passed, closing client c +onnection." ); $last = 0; $selSock->remove($fh); shutdown( $fh, 2 ); close $fh; last; } } } }

                      - Ant
                      - Some of my best work - (1 2 3)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-19 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found