raandom has asked for the wisdom of the Perl Monks concerning the following question:
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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Client socket blocks if server creates subprocess
by BrowserUk (Patriarch) on Dec 06, 2010 at 10:01 UTC | |
by raandom (Acolyte) on Dec 06, 2010 at 19:49 UTC | |
|
Re: Client socket blocks if server creates subprocess
by chrestomanci (Priest) on Dec 05, 2010 at 22:12 UTC | |
by raandom (Acolyte) on Dec 06, 2010 at 08:44 UTC |