summaryrefslogtreecommitdiff
path: root/udpd.pl
blob: c4a38f1de4f8d2433bb732c021910203149b0bf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/perl

##
# Simple UDP Server/Client to display messages in VDR
#
# 22.02.2004 by Thomas Koch <tom at linvdr dot org>
##

use IO::Socket;
use IO::Socket::INET 1.26;
use strict;

my $myself = join("", $0 =~ /^.*\/(.*)/);

# Server
if($myself eq "udpd.pl") {
  my $pid = fork();
  if($pid != 0) {
    exit(0);
  }
  my($Socket) = IO::Socket::INET->new( 
    Proto => 'udp',
    LocalPort => 4711,
    Reuse => 1
  ) || die;				     
  my $message;
  while($Socket->recv($message, 1024)) {
    my($port, $ipaddr) = sockaddr_in($Socket->peername);
    my $hishost = gethostbyaddr($ipaddr, AF_INET);
    system("logger udpd: client $hishost with message \\'$message\\'");
    for(my $z = 0; $z < 3; $z++) {
      for(my $i = 0; $i < 3; $i++) {
        system("svdrpsend.pl mesg $message 2>/dev/null >/dev/null");
      }
      sleep(3);
    }
  }
  exit(0);
}

# Client
if($myself eq "udpc.pl") {
  my $message = join(" ", @ARGV);
  my $Socket = IO::Socket::INET->new(
    PeerAddr => inet_ntoa(INADDR_BROADCAST),
    PeerPort => 4711, 
    Proto => 'udp',
    Broadcast => 1
  ) || die;
  my $result = $Socket->send($message);
}