#!/usr/bin/perl use strict; use warnings; use Socket; use IO::Async::Loop; use IO::Async::Stream; use IO::Socket; use Time::HiRes qw(usleep); # standard event loop my $loop = IO::Async::Loop->new; # notification service socket connection; we only write outgoing my $NOTIFY = IO::Socket::INET->new( PeerHost => $a_local_network_host, PeerPort => $comm_port, Proto => 'tcp', Type => SOCK_STREAM, ReuseAddr => 1, Blocking => 0 ) or warn("Can't connect to NOTIFY: $!\n"); setsockopt($NOTIFY, SOL_SOCKET, SO_KEEPALIVE, 1); # main interface element via IO::Async my $notifystream = IO::Async::Stream->new( handle => $NOTIFY, on_read => sub { my ( $self, $buffref, $eof ) = @_; # here's where we need to handle $eof if the remote goes away if($eof) { # i have tried several things here usleep(200000); # give the remote service some milliseconds to start back up # process fails if remote is not back up, so i know the timeout is 'good enough' for this test # attempt to reconnect. have also tried recreating from scratch $NOTIFY->connect("$a_local_network_host:$comm_port"); # this doesn't seem to have any effect $self->configure(handle=>$NOTIFY); } } ); $loop->add( $notifystream ); # kickstart the event loop $loop->run; ### -- Meanwhile, elsewhere in the code -- ### $notifystream->write("data for notification service\n");