Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't read from socket in perl - possible deadlock?
    text
    copied!<p>My OS is Archlinux with perl 5.14.2. I am just trying to write a little program to accomplish a remote comlile. The program just passes a C source file to the server. The server will call gcc to compile the C code and pass the compiler's message. The client can't receive the compiler's message. I have the message in the server. There is the code:</p> <pre><code>#!/usr/bin/perl -w # oj.pl --- alpha use warnings; use strict; use IO::File; use IO::Socket; use constant MY_TRAN_PORT =&gt; 138000; $| = 1; my $tmpFileToBeCompiled = IO::File-&gt;new ("&gt; tmpFile09090989.c") or die "Can't creat this file"; #if (defined $tmpFileToBeCompiled) { # print $tmpFileToBeCompiled "argh"; # just for test! #} # $fihi-&gt;close; my $port = shift || MY_TRAN_PORT; my $sock_server = IO::Socket::INET-&gt;new (Listen =&gt; 20, LocalPort =&gt; $port, Timeout =&gt; 60, Reuse =&gt; 1) or die "Can't create listening socket: $!\n"; my $tmp = 1; while ($tmp) { next unless my $session = $sock_server-&gt;accept; my $peer = gethostbyaddr ($session-&gt;peeraddr, AF_INET) || $session-&gt;peerhost; warn "Connection from [$peer, $port]\n"; while (&lt;$session&gt;) { print $tmpFileToBeCompiled $_; # if it works, the filehandle should be changed into tmpFile. just fixed. print $session "test!"; } my @lines = `gcc tmpFile09090989.c 2&gt;&amp;1`; foreach ( @lines) { print $session $_ . "test!!!\n"; # $session-&gt;print; } print "OK!"; $tmpFileToBeCompiled-&gt;close; warn "Connecting finished!\n"; $session-&gt;close; $tmp --; } $sock_server-&gt;close; ----------------------------------------end-------------------------------------------------------- -------------------------------------client.pl-------------------------------------------------------- use warnings; use strict; use IO::Socket qw(:DEFAULT); use File::Copy; use constant MY_TRAN_PORT =&gt; 138000; use IO::File; my $host = shift || '127.0.0.1'; my $port = shift || MY_TRAN_PORT; my $socket = IO::Socket::INET-&gt;new("$host:$port") or die $@; my $fh = IO::File-&gt;new("a.c", "r"); my $child = fork(); die "Can't fork: $!\n" unless defined $child; # if (!$child) { # $SIG{CHLD} = sub { exit 0 }; # userToHost(); # print "Run userToHost done!\n"; # $socket-&gt;shutdown(1); # sleep; # } else { # hostToUser(); # print "Run hostToUser done! \n"; # warn "Connection closed by foreign host\n"; # } userToHost(); unless ($child) { hostToUser(); print "Run hostToUser done! \n"; warn "Connection closed by foreign host\n"; $socket-&gt;close; } sub userToHost { while (&lt;$fh&gt;) { # print $_; # for debug print $socket $_; } } sub hostToUser { while (&lt;$socket &gt;) { print $_; } } # copy ("a.c", $socket) or die "Copy failed: $!"; print "Done!"; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload