madper
madper

Reputation: 796

Can't read from socket in perl - possible deadlock?

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:

#!/usr/bin/perl -w
# oj.pl --- alpha


use warnings;
use strict;
use IO::File;
use IO::Socket;

use  constant MY_TRAN_PORT => 138000;
$| = 1;


my $tmpFileToBeCompiled = IO::File->new ("> tmpFile09090989.c") or die "Can't creat this file";

#if (defined $tmpFileToBeCompiled) {
#    print $tmpFileToBeCompiled "argh";         # just for test!
#}
# $fihi->close;

my $port        = shift || MY_TRAN_PORT;

my $sock_server = IO::Socket::INET->new (Listen         => 20,
                                         LocalPort      => $port,
                                         Timeout        => 60,
                                         Reuse          => 1)
    or die "Can't create listening socket: $!\n";

my $tmp = 1;
while ($tmp) {
    next unless my $session = $sock_server->accept;

    my $peer = gethostbyaddr ($session->peeraddr, AF_INET)
        || $session->peerhost;
    warn "Connection from [$peer, $port]\n";

    while (<$session>) {
        print $tmpFileToBeCompiled $_;              # if it works, the filehandle should be changed into tmpFile.  just fixed.
        print $session "test!";

    }

    my @lines = `gcc tmpFile09090989.c 2>&1`;

    foreach ( @lines) {
        print $session  $_ . "test!!!\n";
     #   $session->print;
    }


    print "OK!";
    $tmpFileToBeCompiled->close;

    warn "Connecting finished!\n";
    $session->close;
    $tmp --;
}

$sock_server->close;

----------------------------------------end--------------------------------------------------------
-------------------------------------client.pl--------------------------------------------------------
use warnings;
use strict;

use IO::Socket qw(:DEFAULT);
use File::Copy;
use constant MY_TRAN_PORT => 138000;
use IO::File;

my $host = shift || '127.0.0.1';
my $port = shift || MY_TRAN_PORT;

my $socket = IO::Socket::INET->new("$host:$port") or die $@;

my $fh = IO::File->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->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->close;

}

sub userToHost {
    while (<$fh>) {
#    print $_;    # for debug
    print $socket $_;
    }
}

sub hostToUser {
    while (<$socket >) {
    print $_;    
    }
}
# copy ("a.c", $socket) or die "Copy failed: $!";
print "Done!";

Upvotes: 3

Views: 1320

Answers (2)

themel
themel

Reputation: 8895

yko nailed it, but let me just suggest that your task will be solved in a much easier and more maintainable way by a shell script running from inetd.

Upvotes: 1

yko
yko

Reputation: 2710

  1. You don't need to fork in client. At all. Just like themel said
  2. You have error in client code: <$socket > should be <$socket>
  3. You need to notify server that you have written all data and server can start compilation. Otherwise server will stuck at while (<$session>) forever. To achieve this you could call shutdown($socket, 1) which means you finished writing. See perldoc -f shutdown

Final prototype (very rough) could look like this: https://gist.github.com/19b589b8fc8072e3cfff

Upvotes: 2

Related Questions