#!/usr/bin/perl use warnings; use strict; use IO::Socket::INET; $/ = "\r\n"; my $socket = IO::Socket::INET->new( PeerAddr => 'localhost', PeerPort => 3009, Proto => 'tcp', ) or die "Couldn't open socket: $!"; print "Registering commands.\n"; print $socket "REG commands\n"; while (<$socket>) { chomp; print "Received $_\n"; next unless (/^commands/); my $reply = lsreg(); # Put the reply after the second field, removing the first field. s/^([^\t]*\t)([^\t]*)/$2 $reply/ || next; print "Saying ", $_, "\n"; print $socket $_, "\n"; } sub lsreg { my $lssocket = IO::Socket::INET->new( PeerAddr => 'localhost', PeerPort => 3009, Proto => 'tcp', ) or die "Couldn't open socket: $!"; print $lssocket "LSREG\r\n"; my $commands = <$lssocket>; close $lssocket; $commands =~ s/\r\n$// || $commands =~ s/\n$//; my @commands = grep {!/^LSREG$|^REG$|^UNREG$|^ISREG$|^MSG$|^JOIN$|\@/} split(/ /, $commands); print join(', ', sort @commands), "\n"; return(join(', ', sort @commands)); }