sub read_data { # accept data from the socket and put it on the queue my( $qin, $qout, $socket ) = @_; while ( 1 ) { ## Don't enter a read state until your pretty sure there will be something ## to fetch. The loop count (10) x the sleep value (1) define the device ## data frequency for( 1 .. 10 ) { ## if there are any commands pending, send them to the GPS device while( my $cmd = $qin->pending ) { print $socket $cmd; } sleep 1; ## or usleep 0.1 or 0.01 etc. ## depending upon the urgency of your cmd requirements } my $data = <$socket>; last unless defined $data; print "$data"; ## Process data from connected devices $qout->enqueue( time . ' ' . $data ); } shutdown $socket, 2; close $socket; }