#!/usr/bin/perl -w ## ## $HOME/bin/mdaemon.pl ## Sebastian Helms ## Last changed: 02.01.2007 10:12 ## ## runs mplayer and forwards commands received via telnet use IO::Socket; use IO::Handle; use Net::hostent; # for OO version of gethostbyaddr my $quit = 0; my $exec = "/usr/local/bin/mplayer"; # argument quiet, so we don't get all status information # argument slave, so mplayer accepts commands, not keypress input my $arg = "-slave -quiet -playlist /srv/ftp/filme/playlist"; print "own PID: $$\n"; while (1) { $SIG{HUP} = 'IGNORE'; # works -> player is started open PLAYER, "|$exec $arg" or die "Can't start player."; PLAYER->autoflush(1); # works also, tcp connection is possible for a short time my $PORT = 4500; # pick something not in use my ($client, $client_address, $client_port, $client_ip, $client_dq); my $server = IO::Socket::INET->new( Proto => 'tcp', LocalAddr => 'localhost', LocalPort => $PORT, Listen => SOMAXCONN, Reuse => 1); die "can't setup server" unless $server; print "[Server $0 accepting clients]\n\n"; # we get into here while (($client, $client_address) = $server->accept()) { $client->autoflush(1); ($client_port, $client_ip) = sockaddr_in($client_address); $client_dq = inet_ntoa($client_ip); print "[Accepted connection from $client_dq:$client_port]\n"; open LOG, ">/tmp/mdaemon.log"; LOG->autoflush(1); while ( <$client> ) { chomp; last if //; if (/kill/) { print PLAYER "quit\n"; close PLAYER; $quit = 1; last; } print PLAYER "$_\n"; print LOG "$_\n"; } close LOG; close $client; print "[Closed connection to $client_dq:$client_port]\n\n"; last if $quit == 1; } $server->shutdown(); print "[Server quitting.]\n"; }