server/comm-socket.pl

47 lines
679 B
Perl
Raw Normal View History

2014-01-20 04:53:31 +00:00
#!/usr/bin/env perl
use v5.10.1;
use IO::Select;
use IO::Socket::UNIX;
use threads;
die("Give at least the socket file as argument") if (! @ARGV);
2014-01-20 09:58:59 +00:00
my $sock_path = shift @ARGV;
2014-01-20 04:53:31 +00:00
my $socket = IO::Socket::UNIX->new(
Type => SOCK_STREAM,
2014-01-20 09:58:59 +00:00
Peer => $sock_path,
2014-01-20 04:53:31 +00:00
);
2014-01-20 09:58:59 +00:00
die "Can't read socket ($sock_path): $!" unless $socket;
if (@ARGV)
{
while (my $arg = shift @ARGV)
{
say $socket $arg;
}
}
2014-01-20 04:53:31 +00:00
my $s = IO::Select->new();
$s->add(\*STDIN);
$s->add($socket);
while ($s->count())
{
for my $rd ($s->can_read(0.25))
{
my $line = <$rd>;
chomp($line);
if ($rd == \*STDIN) {
say $socket $line;
}
elsif ($rd == $socket) {
say $line;
}
}
}