Persisten TCP Connection for Win32

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top