B
Brad Walton
I am trying to setup a persistent TCP connection between 2 Perl scripts
(using IO::Socket::INET). I have attempted establishing a pipe, but when the
server-side is broken, then connection will not re-establish. This seems to
work ok on Linux, but not Windows.
Client Code (from package):
---------------------------------------------
use strict;
use warnings;
use Storable qw(nstore_fd);
use IO::Socket::INET;
my $socket;
$SIG{PIPE} = sub {$socket = undef};
.... declare some globals ...
#=========================================
# sconnect
#
# Sets up persistent connection
#=========================================
sub sconnect {
my $rv;
$rv = IO::Socket::INET->new( PeerAddr => $_stats_ip, PeerPort =>
$_stats_port, Proto => "tcp", Type => SOCK_STREAM);
return $rv;
}
#======================================
# stats_connect
#
# send single-line data
#======================================
sub stats_connect ($) {
my $stats_data = shift;
return if (!$_doing_stats);
my %PACKET = ();
$PACKET{'data'}{'type'} = $stats_data;
if (!$socket) { $socket = sconnect(); }
nstore_fd(\%PACKET, $socket) if ($socket);
return;
}
---------------------------------------------
Server Code
---------------------------------------------
use strict;
use warnings;
use Storable qw(fd_retrieve);
use IO::Socket::INET;
.... declare some globals ...
my $server = IO::Socket::INET->new( LocalPort => $CONFIG{'fb port'},
Type => SOCK_STREAM,
Reuse => 1,
Listen => 10 ) # or SOMAXCONN
or die "Couldn't start server on port $CONFIG{'fb port'}: $!\n";
print " Initialized and ready to collect data\n";
print "--------------------------------------------------------------\n";
while (my $client = $server->accept()) {
eval {
while (my %PACKET = %{ fd_retrieve $client }) {
.... do stuff ...
}
}
}
---------------------------------------------
Thanks for any help!
Brad
(using IO::Socket::INET). I have attempted establishing a pipe, but when the
server-side is broken, then connection will not re-establish. This seems to
work ok on Linux, but not Windows.
Client Code (from package):
---------------------------------------------
use strict;
use warnings;
use Storable qw(nstore_fd);
use IO::Socket::INET;
my $socket;
$SIG{PIPE} = sub {$socket = undef};
.... declare some globals ...
#=========================================
# sconnect
#
# Sets up persistent connection
#=========================================
sub sconnect {
my $rv;
$rv = IO::Socket::INET->new( PeerAddr => $_stats_ip, PeerPort =>
$_stats_port, Proto => "tcp", Type => SOCK_STREAM);
return $rv;
}
#======================================
# stats_connect
#
# send single-line data
#======================================
sub stats_connect ($) {
my $stats_data = shift;
return if (!$_doing_stats);
my %PACKET = ();
$PACKET{'data'}{'type'} = $stats_data;
if (!$socket) { $socket = sconnect(); }
nstore_fd(\%PACKET, $socket) if ($socket);
return;
}
---------------------------------------------
Server Code
---------------------------------------------
use strict;
use warnings;
use Storable qw(fd_retrieve);
use IO::Socket::INET;
.... declare some globals ...
my $server = IO::Socket::INET->new( LocalPort => $CONFIG{'fb port'},
Type => SOCK_STREAM,
Reuse => 1,
Listen => 10 ) # or SOMAXCONN
or die "Couldn't start server on port $CONFIG{'fb port'}: $!\n";
print " Initialized and ready to collect data\n";
print "--------------------------------------------------------------\n";
while (my $client = $server->accept()) {
eval {
while (my %PACKET = %{ fd_retrieve $client }) {
.... do stuff ...
}
}
}
---------------------------------------------
Thanks for any help!
Brad