in reply to Two TCP Connections, one script
A working example using threads complete with disconnect recovery:
#! perl -slw use strict; use threads; use Thread::Queue; use IO::Socket; my $Q = new Thread::Queue; my @threads = map async { while( 1 ) { my $s = IO::Socket::INET->new( $_ ) or die $!; while( my $input = <$s> ) { $Q->enqueue( $input ); } sleep 5; } }, 'localhost:12345', 'localhost:6789'; while( my $input = $Q->dequeue ) { chomp $input; if( $input =~ m[Z:] ) { ## Deal with telemetry print "Got telemetry: '$input'"; } else { ## Deal with waypoint; print "Got waypoint: '$input'"; } }
A little trace showing reconnect for both servers:
C:\test>678330.pl Got telemetry: 'X:-77.64038 Y:-153.75366 Z: 15910' Got waypoint: 'X:-171.05713 Y:051.82251' Got telemetry: 'X:-42.74780 Y:028.83911 Z: 28944' Got waypoint: 'X:-38.79272 Y:-104.06250' Got telemetry: 'X:057.34863 Y:-125.10132 Z: 2259' Got waypoint: 'X:-27.70752 Y:073.97095' Got telemetry: 'X:-107.01782 Y:-63.42407 Z: 11333' Got waypoint: 'X:119.52026 Y:122.77222' Got telemetry: 'X:065.73120 Y:076.75049 Z: 7514' Got waypoint: 'X:-101.51367 Y:059.56787' Got waypoint: 'X:077.84912 Y:-00.31860' Got waypoint: 'X:000.24170 Y:-07.85522' Got waypoint: 'X:-179.58252 Y:-50.33936' Got waypoint: 'X:-138.26294 Y:-179.98901' Got waypoint: 'X:-20.14893 Y:062.01782' Got waypoint: 'X:-86.61621 Y:-63.24829' Got waypoint: 'X:-99.17358 Y:-173.48511' Got telemetry: 'X:-122.01416 Y:036.21094 Z: 17707' Got waypoint: 'X:-65.35767 Y:-42.01172' Got telemetry: 'X:-27.78442 Y:123.21167 Z: 11011' Got waypoint: 'X:094.70215 Y:174.35303' Got telemetry: 'X:-57.84302 Y:034.00269 Z: 11311' Got waypoint: 'X:-83.51807 Y:-146.19507' Got telemetry: 'X:-09.01978 Y:035.83740 Z: 6095' Got telemetry: 'X:-14.65576 Y:176.96777 Z: 14657' Got telemetry: 'X:-97.55859 Y:-81.47461 Z: 28815' Got telemetry: 'X:-150.65552 Y:087.11060 Z: 14598' Got telemetry: 'X:-113.20313 Y:-15.20508 Z: 15945' Got telemetry: 'X:-127.38647 Y:098.18481 Z: 1425' Got telemetry: 'X:022.19238 Y:-145.00854 Z: 14309' Got waypoint: 'X:-129.70459 Y:125.38696' Got telemetry: 'X:-101.64551 Y:-58.45825 Z: 15181' Got waypoint: 'X:-14.16138 Y:171.74927' Got telemetry: 'X:-88.86841 Y:-92.66968 Z: 3557' Got waypoint: 'X:-121.08032 Y:002.66968' Got telemetry: 'X:125.14526 Y:-29.19067 Z: 13236' Got waypoint: 'X:155.89600 Y:-78.14575' Got telemetry: 'X:094.61426 Y:-44.34082 Z: 6590' Got waypoint: 'X:-74.31152 Y:110.48950' Terminating on signal SIGINT(2)
|
|---|