I have simple client and server scripts based on IO::Socket. Server just prints something into socket. Client just reads that from socket. But! If the server creates a subprocess (using system() call) before (!) closing client socket then the client will block on read until subprocess finishes. The point is that the server finishes immediately (subprocess is created that way). The problem occurs only if server is on Windows. BTW: I created the same sample on java and it works perfectly everywhere.

Server:

########### use strict; use IO::Socket; my $Unix; if ($^O =~ m/unix|linux|sunos|solaris|freebsd|aix/i) { $Unix = 1 } else { $Unix = 0 } my $sock = new IO::Socket::INET ( LocalPort => '8181', Proto => 'tcp', Listen => SOMAXCONN, ReuseAddr => $Unix, ); die "Could not create socket: $!\n" unless $sock; $| = 1; my $client = $sock->accept(); print "Message1\n"; print $client "Message1\n"; # You can release client here. if not - later it will be blocked. #close($client); # Something that wastes time + separate process creator my $cmd; if ($Unix) { $cmd = 'pause 10'; $cmd .= ' &'; } else { $cmd = 'pause'; $cmd = 'cmd.exe /c start cmd.exe /c ' . $cmd; } # This will block both Unix and Win clients if the server is on Win system($cmd); close($client); $sock->shutdown(2); close($sock);

Client:

########### use strict; use IO::Socket; my $peeraddr = shift @ARGV || 'localhost'; print 'Connecting to: ' . $peeraddr . "\n"; my $sock = new IO::Socket::INET ( PeerAddr => $peeraddr, PeerPort => '8181', Proto => 'tcp', ); die "Could not create socket: $!\n" unless $sock; $| = 1; # it will block here while (<$sock>) { print; } print "done\n"; close($sock);

In reply to Client socket blocks if server creates subprocess by raandom

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.