I am having trouble with closing a socket on Win32. It's an XML-RPC server running on port 8080. I have one routine in which the server is to launch a restart-script and then die. It works fine on UNIX. It seems to work fine on Win32...except that the port stays unavailable...so that the restart dies for lack of a port.

In the server, to restart, I have this...

my $prefork_pid = $$; if ( defined( my $kid = fork ) ) { unless ($kid) { # Launch the external update/restart script. exec( "perl", "$script_path", "$local_port", ); } print "This always prints...\n"; # Try dying gracefully first, then less so. kill 15, $prefork_pid; sleep 5; kill 2, $prefork_pid; sleep 5; kill 9, $prefork_pid; print "This never prints... So it died, alright.\n"; }

And in the restart script, I have this...

my ($local_port) = @ARGV; # When on Win32, nap until OS clears its port. my $i = 50; while ($i && ($^O =~ /Win32/i)) { my @netstat = split "\n", `netstat.exe -a -p tcp`; print "Waiting up to $i passes...\n"; foreach (@netstat) { next unless $_ =~ $local_port; print "$i: \t$_\n"; sleep 60 if $_ =~ /(ESTABLISHED|LISTENING|WAIT)/; } --$i; }

The wait-loop is an experiment...to see how long the port stays unavailable. It stays that way a long, long time...maybe forever.

Not shown beyond the wait-loop is the restart code, which launches an updated version of the server on the same port. This works in UNIX, but is futile on Win32...because the port never clears.

Now I read about something called Win32::Process which I should use instead of fork. So my question is: With what would I supplant the above for Win32 using Win32::Process? What is the best way?

Thanks,


In reply to Socket left open on Win32 after fork-and-die... by aplonis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.