Dear huck.
Currently I have only one connection.(I expect to have more in the future).
I need to request device params and keep this connection for 5min (as polling time) and then release it.
Device will reconnect inmediatly after closing tcp socket.
This single connection is crashing my service.
Im traying to apply all concepts you mentioned but it is really hard for me , besides , the code you provided is not working.
Any way ; this my new version with my own eval blocks:
#!/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 $client; my $ident='tcp-mqtt_server'; my $logopt='ndelay'; my $facility='LOG_USER'; my $th_id; my $topic; my $value; 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 ); # Bind to listening address and port $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) { # Waiting for new client connection. $client_socket = $socket->accept(); # Push new client connection to it's own thread 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"); eval{ my $datestring = strftime "%a %b %e %H:%M:%S %Y", localtim +e; printf("$datestring\n"); print $client_socket "\n"; print $client_socket "VLIN\n"; while( my $buffer = <$client_socket> ) { print $buffer; my $value = substr $buffer, 0,-2; $mqtt->retain("minimon/VLIN" => $buffer); syslog(LOG_INFO,"VLIN: $buffer"); last; } sleep(300); }; if($@){ syslog(LOG_INFO,"tcp-mqtt exception:$@->getErrorMessage()") +; } $client_socket->shutdown(2); #$client_socket->close(); print "Client exit from ".$user{peer_address}.":".$user{peer_port} +."\n"; # Client has exited so thread should exit too threads->exit(); } # Start the Main loop Main();
Thanks again for your words.

In reply to Re^2: Improve my tcp-mqtt server by leostereo
in thread Improve my tcp-mqtt server by leostereo

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.