Add socket communication
This commit is contained in:
parent
dee5b5fbb7
commit
2b27340b46
5 changed files with 96 additions and 15 deletions
36
comm-socket.pl
Normal file
36
comm-socket.pl
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#!/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);
|
||||
|
||||
my $socket = IO::Socket::UNIX->new(
|
||||
Type => SOCK_STREAM,
|
||||
Peer => $ARGV[0],
|
||||
);
|
||||
|
||||
die "Can't create socket: $!" unless $socket;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in a new issue