http://qs321.pair.com?node_id=11118272

leostereo has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys:
Some days ago I wrote a multithread tcp server.
It should accept incoming tcp connections in a while loop, then request some data and push to my mosquitto every 5 minutes.
It is working ok but after some ramdom time, 2 , 3 ,10 hours it stop working and can not find out why.
So , questions are:
How can I find out what is making my server exi ?
Where/how should I use eval block to handle the exception?
Any other advice would be wellcome, of course.
Thanks !!!
This is my code:
#!/usr/bin/perl use strict; use warnings; use IO::Socket::INET; use threads; use Net::MQTT::Simple; use POSIX qw(strftime); use Sys::Syslog qw(:DEFAULT :standard :macros); my $mqtt; my $ident='tcp-mqtt_server'; my $logopt='ndelay'; my $facility='LOG_USER'; my $th_id; print "parent $$\n"; sub Main { $mqtt = Net::MQTT::Simple->new("localhost:1883"); openlog($ident, $logopt, $facility); # don't forget this # flush after every write $| = 1; my ( $socket, $client_socket ); $socket = new IO::Socket::INET ( LocalHost => '0.0.0.0', LocalPort => '1001', Proto => 'tcp', Listen => 5, Reuse => 1 ) or die "Could not open socket: ".$!."\n"; print "SERVER Waiting for client connections...\n"; syslog(LOG_INFO,"tcp-mqtt server starting"); my @clients = (); while(1) { $client_socket = $socket->accept(); push ( @clients, threads->create( \&clientHandler, $client_soc +ket ) ); foreach ( @clients ) { if( $_->is_joinable() ) { $_->join(); } } } $socket->close(); return 1; } sub clientHandler { my ($client_socket) = @_; my %user = (); $user{peer_address} = $client_socket->peerhost(); $user{peer_port} = $client_socket->peerport(); $user{local_port} = $client_socket->sockport(); $th_id = threads->tid(); print "Client ".$user{peer_address}.":".$user{peer_port}.":".$user +{local_port}."\n"; syslog(LOG_INFO,"Client $user{peer_address}:$user{peer_port}:$u +ser{local_port}:$th_id is connected"); while(1){ my $datestring = strftime "%a %b %e %H:%M:%S %Y", localtime; printf("$datestring\n"); print $client_socket "TEMP\n"; while( my $buffer = <$client_socket> ) { print $buffer; $mqtt->retain("minimon/temp" => $buffer); syslog(LOG_INFO,"temperature: $buffer"); last; } print $client_socket "VBAT\n"; while( my $buffer = <$client_socket> ) { print $buffer; $mqtt->retain("minimon/vbat" => $buffer); last; } print $client_socket "VLIN\n"; while( my $buffer = <$client_socket> ) { print $buffer; $mqtt->retain("minimon/vlin" => $buffer); last; } sleep(300); } print "Client exit from ".$user{peer_address}.":".$user{peer_port} +."\n"; threads->exit(); } # Start the Main loop Main();
BTW , Im launching this doing:
nohup ./tcp-mqtt_server_multiclient.pl 2> nohup.out &
So I can check for errors on nohup.out.