#!/bin/perl
# Perl client-socket example

# this version does not use the IO::Socket library, so it's more like C than Java

use Socket;
require "byteordering.pl";  # this is a set of routines I just defined

my ($SERVER,$PORT) = @ARGV;
my $saddr;
my $buffer = "";   # binary (string) data buffer;
my $d = 3.14;      # double value
my ($x, $y, $i, $j); # utility

$byteorder = endiancheck();

socket(cfd,AF_INET,SOCK_STREAM,getprotobyname('tcp')) || die "error";
$saddr = sockaddr_in ($PORT, inet_aton($SERVER)); # $SERVER can be ip or name
connect(cfd,$saddr) || die "connection error";

sysread(cfd,$x,4,0);
$x = ntohl($x);
$x = unpack("i",$x); # convert from binary
print "expecting ", $x, " bytes\n";
sysread(cfd,$buffer,$x,0);
$y = pack("d",$d);   # pack "3.14" into binary format
syswrite(cfd,$y,8,0);
print "received $buffer \n";

close(cfd);


