[LON-CAPA-cvs] cvs: modules /gerd/scantron scantronserver.pl
www
lon-capa-cvs@mail.lon-capa.org
Mon, 24 May 2004 00:11:48 -0000
www Sun May 23 20:11:48 2004 EDT
Added files:
/modules/gerd/scantron scantronserver.pl
Log:
Device server automatically connects to server.
Server takes control of scanner, reads data.
Index: modules/gerd/scantron/scantronserver.pl
+++ modules/gerd/scantron/scantronserver.pl
use strict;
use POSIX qw(:sys_wait_h);
use IO::Socket;
my $server_port=5664;
my $server=IO::Socket::INET->new(LocalPort => $server_port,
Type => SOCK_STREAM,
Proto => 'tcp',
Reuse => 1,
Listen => 10);
sub REAPER {
1 until (-1 == waitpid(-1, WNOHANG));
$SIG{CHLD} = \&REAPER; # unless $] >= 5.002
}
$SIG{CHLD} = \&REAPER;
my $pid;
while (my $client = $server->accept()) {
next if $pid = fork; # parent
die "fork: $!" unless defined $pid; # failure
# otherwise child
my $caller = getpeername($client);
my ($port,$iaddr);
if (defined($caller) && length($caller) > 0) {
($port,$iaddr)=unpack_sockaddr_in($caller);
} else {
exit;
}
my $hostname;
my $clientip;
if (defined($iaddr)) {
$clientip=inet_ntoa($iaddr);
$hostname=gethostbyaddr($iaddr,AF_INET);
}
print "\n$$: Connected $hostname $clientip\n";
print $client "0\n";
while (my $garbage=<$client>) {
if ($garbage=~/\w/) { last; }
}
print "Communicating\n";
# Put into escrow mode
print $client "e\n";
while (my $line=<$client>) {
&analyse($client,$line);
}
print "\n$$: Disconnect $hostname $clientip\n";
exit; # child leaves
}
#
# Analyse any data that comes from the scanner
#
sub analyse {
my ($client,$data)=@_;
print "\nData: $data";
print $client "3\n";
}