#!/usr/bin/perl
#$Id: gpibclient,v 1.5 2011/03/30 13:13:45 sofc Exp $
use strict;
use warnings;
use Socket;

print client(@ARGV),"\n";

sub client
{
    my ($remote,$port, $iaddr, $paddr, $proto, $line);
    $remote  = 'localhost';
    $port   = 12345;
    if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
    die "No port" unless $port;
    $iaddr   = inet_aton($remote)               || die "no host: $remote";
    $paddr   = sockaddr_in($port, $iaddr);
    my $out = '';

    $proto   = getprotobyname('tcp');
    eval {
        socket(SOCK, PF_INET, SOCK_STREAM, $proto)  || die "socket: $!";
        connect(SOCK, $paddr)    || die "Could not connect to $remote port $port: $!";
        select(SOCK);
	$|=1;
        select(STDOUT);
        print SOCK join("\t",@_);
        while (defined($line = <SOCK>))
        {
            $out = $out.$line;
        }
        close (SOCK)            || die "close: $!";
    };
    if ($@)
    {
	warn $@;
        return undef;
    }
    return $out;
}
