1#!/usr/bin/perl -w 2# 3# Contributed by Bastiaan Bakker for SOCKETMAP 4# $Id: socketmapClient.pl,v 1.1 2003/05/21 15:36:33 ca Exp $ 5 6use strict; 7use IO::Socket; 8 9die "usage: $0 <connection> <mapname> <key> [<key2> ...]" if (@ARGV < 3); 10 11my $connection = shift @ARGV; 12my $mapname = shift @ARGV; 13 14my $sock; 15 16if ($connection =~ /tcp:(.+):([0-9]*)/) { 17 $sock = new IO::Socket::INET ( 18 PeerAddr => $1, 19 PeerPort => $2, 20 Proto => 'tcp', 21 ); 22} elsif ($connection =~ /((unix)|(local)):(.+)/) { 23 $sock = new IO::Socket::UNIX ( 24 Type => SOCK_STREAM, 25 Peer => $4 26 ); 27} else { 28 die "unrecognized connection specification $connection"; 29} 30 31die "Could not create socket: $!\n" unless $sock; 32 33while(my $key = shift @ARGV) { 34 my $request = "$mapname $key"; 35 netstringWrite($sock, $request); 36 $sock->flush(); 37 my $response = netstringRead($sock); 38 39 print "$key => $response\n"; 40} 41 42$sock->close(); 43 44sub netstringWrite { 45 my $sock = shift; 46 my $data = shift; 47 48 print $sock length($data).':'.$data.','; 49} 50 51sub netstringRead { 52 my $sock = shift; 53 my $saveSeparator = $/; 54 $/ = ':'; 55 my $dataLength = <$sock>; 56 die "cannot read netstring length" unless defined($dataLength); 57 chomp $dataLength; 58 my $data; 59 if ($sock->read($data, $dataLength) == $dataLength) { 60 ($sock->getc() eq ',') or die "data misses closing ,"; 61 } else { 62 die "received only ".length($data)." of $dataLength bytes"; 63 } 64 65 $/ = $saveSeparator; 66 return $data; 67} 68