Raw Sockets, Spectra Client

Here is an example for how a client, using the Perl socket interface, communicates with Spectra running in the server mode.

#!/usr/bin/perl -w
#
# To test this piece of code: Start Spectra on pal11, enter
# the server mode ...
#
#   SPECTRA> server 7777/verbose
#
# ... and run this code. 
#
# # client.pl pal11 7777
#
use strict; 
#
# need this because the file handle $sck is 'variable'
#
no strict 'refs'; 

use Socket;
use Fcntl; 
  
my ($remote, $port, $iaddr, $paddr, $proto, $line); 

$remote = shift || 'pal11'; 
$port = shift || 7777; 
$port = getservbyname( $port, 'tcp') if( $port =~ /\D/); 
die " no port " unless $port; 

$iaddr = inet_aton( $remote) or die "no host $remote"; 
$paddr = sockaddr_in( $port, $iaddr); 
$proto = getprotobyname( 'tcp'); 
#
# allow for 'non-static' filehandles
#
my $sck = "SOCK"; 
socket( $sck, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; 
connect( $sck, $paddr) or die "connect: $!";
#
# set the socket to 'blocking'
#
my $flags = fcntl( $sck, F_GETFL(), 0);
$flags  &= ~O_NONBLOCK();    # Clear non-blocking, but preserve other flags
fcntl( $sck, F_SETFL(), $flags);
#
# send the command
# 
my $com = "*=2*3\n"; 
print " sending $com\n"; 
syswrite( $sck, $com, length($com), 0); 
#
# test whether we have input
#
my $rin = my$win = my $ein = ""; 
vec( $rin, fileno( $sck), 1) = 1; 
$ein = $rin | $win; 
my $tmo = 2.3; 
(my $nfound, my $remaining) = select( $rin, $win, $ein, $tmo); 
print " nf $nfound remaining time $remaining \n"; 
die 'no input' if( !$nfound);
#
# get the input
#
my $buffer = " " x 10; 
my $offset = 0; 
my $len = sysread( $sck, $buffer, 10, $offset); 

print " received $buffer\n"; 

close( $sck); 
exit;