Hello to all

I have a tcp server which is accepting data from gprs clients and one client from localhost, which is serving command to other clients.
The clients are always connected to the server, except when is some network outage or other gprs disconnections. Then, clients reconnects to the server

And there is my problem: Old threads remains connected to the server and I need to get rid of them!
Obviously I'm a novice with Perl, so I posted my tcp server code to get also some opinions for improvement.

#!/usr/bin/perl use warnings; use strict; use IO::Socket; use threads; use threads::shared; $|++; print "$$ Server started\n";; # do a "top -p -H $$" to monitor server + threads my $buf2; my $SyncID; my $SyncHeader; my $UnitID; our @clients : shared; @clients = (); our $k; our $m; our $n; our $p; our $dup=0; our $sel_cli2; my @Mdevices; my $ref; my $peerC = 1;
my $server = new IO::Socket::INET( Timeout => 500, Proto => "tcp", LocalPort => 8000, Reuse => 1, Listen => 5 ); my $num_of_client = -1; while (1) { my $client; do { $client = $server->accept; } until ( defined($client) ); my $peerhost = $client->peerhost(); $peerC = $peerhost; if ($peerC ne '127.0.0.1') { my $template = 'vvV'; read $client, my $sync_packet,8 or die "Couldn't read from client: $!"; ($SyncHeader,$SyncID,$UnitID) = unpack $template, $sync_packet; print "accepted a client $client, $UnitID, $peerhost, id = ", ++$n +um_of_client, "\n"; } else { $UnitID = 888; print "accepted a client $client, $peerhost, id = ", ++$num_of_client, + "\n"; } my $fileno = fileno $client; push (@clients, $fileno); for ($k=0; $k<=$#Mdevices; $k++) { if ($Mdevices[$k][0] eq $UnitID) # searching existi +ng UnitID in Array { $dup =1; print "Duplicate found $Mdevices[$k][0 +] |"; $Mdevices[$k][1] = $fileno; # update e +xisting UnitID with new fileno } } if ($dup eq 1) { print "NO changes |"; $dup = 0; } else { for ($n=0; $n<=$#Mdevi +ces; $n++) # chech if exist some entry with old fileno, should be del +eted { if ($Mdevices[ +$n][1] eq $fileno) { print +"deleting |"; $Mdevi +ces[$n][1] = 0; # set old entry's fileno to 0 to not interfere. } } push (@Mdevices, [$UnitID, $fileno]); +# add new device with fileno to array } use Data::Dumper; print Dumper(\@Mdevices); #spawn a thread here for each client my $thr = threads->new( \&processit, $client, $fileno, $peerhost ) +->detach(); }
sub processit { my ($lclient,$lfileno,$lpeer) = @_; #local client my $sel_cli_id = $num_of_client; if($lclient->connected){ while(<$lclient>) { if ($peerC eq '127.0.0.1') # tcp client from localhost + is used just to send commands to other connected clients { my(@comm) = split(/:/, $_); my $sel_clix = $comm[0]; my $commandC = $comm[1]; for ($p=0; $p<=$#Mdevices; $p++) { if ($Mdevices[$p][0] eq $sel_clix) { $sel_cli2 = $Mdevices[$p][1]; } } foreach my $fn ($sel_cli2) { if ($sel_cli2) { open my $fh, ">&=$fn" or warn +$! and die; print "\n $lclient $sel_cli2 C +ommand sent to: $sel_clix Command: $commandC"; print $fh "$commandC\n" } else { print "\n $lclient Device: $se +l_clix NOT avaiable to receive command\n"; } } } else { print "COMMAND RESPONCE: $_"; } } } #close filehandle before detached thread dies out close( $lclient); #remove multi-echo-clients from echo list @clients = grep {$_ !~ $lfileno} @clients; }

In reply to Non closing sockets - threads by igor1212

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.