Hi All,

I have an issue with a small piece of code.  I am using IO::Socket::INET to
accept client connections from an socket, and then hand the connection off
to an newly created thread.  As soon as an client disconnects (or interrupts
the tcp session), the script will exit completely.  No errors, and the
application doesn't exist the while loop either (the print after the loop
isn't executed).  It almost seems that the main thread is killed?

my $Queue :shared;                      # Shared reference to our Queued
work
my $num_of_threads = 2;                 # Maximum number of threads to start
my @Threads;                            # An array holding an reference to
our threads
my $SocketAddress = "198.19.255.11";
my $SocketPort = 2000;

# This is the main threads serving the socket connections
sub ProcessClient {
  my ($Client,$Queue) = @_;
  while ($Client) {
    my $MessageID = <$Client>;
    if ($MessageID && length($MessageID) > 8) {
      $MessageID =~ s/\r?\n$//;
      my $Result = $Queue->enqueue_and_wait($MessageID);
      my ($Content) = @$Result;
      print $Client $Content;
    } else {
      print $Client "Invalid Message-ID\n.\n";
    }
  }
}

# This is the main threads fetching
sub ThreadWorking {
  my ($Queue) = @_;
  print "Starting Fetcher Thread: " . $$ . "." . threads->tid . "\n";
  ... code ommited ...
}

### Main Processing Section
# Flush buffers after ever write, and setup some other parameters
$| = 1;
select(STDOUT);
setpriority (0,0,20);

# Start the main queue and threads
$Queue = Thread::Queue::Duplex->new(ListenerRequired => 0,
                                    MaxPending       => 1024);
for my $Count (1 .. $num_of_threads) {
  my $QueueWorker = threads->create(\&ThreadWorking, $Queue);
  $QueueWorker->detach();
  push (@Threads, $QueueWorker);
}

print "Waiting for Queue Listeners to become ready...\n";
$Queue->wait_for_listener();
sleep(5);

# Start our main listening socket
my $Socket = IO::Socket::INET->new(LocalAddress => $SocketAddress,
                                   LocalPort    => $SocketPort,
                                   Proto        => "tcp",
                                   Type         => SOCK_STREAM,
                                   Listen       => SOMAXCONN,
                                   ReuseAddr    => 1) or die "Can't create
listen socket: $!";
$Socket->autoflush(1);
while (1) {
  my $Client;
  do {
    $Client = $Socket->accept();
    $Client->autoflush(1);
  } until (defined($Client));
  my $ClientThread = threads->create(\&ProcessClient, $Client,
$Queue)->detach;
  push (@Threads, $ClientThread);
  print "Connection received from " . $Client->peerhost() . ":" .
$Client->peerport() . "\n";
}
print "exited while loop\n";


Output:
root@netty:/opt/bin# ./threads 
Starting Fetcher Thread: 17963.1
Waiting for Queue Listeners to become ready...
Starting Fetcher Thread: 17963.2
OK:  Tread: 17963.001, Time: 1.212, Ready to work
OK:  Tread: 17963.002, Time: 1.210, Ready to work
Connection received from 198.19.255.3:37991
root@NNTPWEB01:/srv/nntp/bin#

I've tried various ways to do the while loop, but pretty much everything is
showing the same results, which is why I'm thinking more towards that
there's an threading issue... 

I would appreciate any assistance.

--
Chris.



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to