[NTLUG:Discuss] Forking server in bash posible ? (fun hack ...no apparent value )
gan hawk
ganhawk1 at hotmail.com
Wed Mar 31 00:18:57 CST 2004
Hi,
This is something that has no value but I have done to amuse myself. So
read on if you also want to waste some time ;)
I am trying to learn perl and saw this perl script to create a streaming MP3
server on perl monks. I wanted to do the same in bash and here is what I
came up with (see below)... Now I was wondering if it was possible to make
it a forking server. Like listen and when some connection comes in, fork or
spaw a child process to do the work and continue listening. I think its
possible with netcat but I cannot figure it out. Anyways if someone can do
that it would be fun.
#!/bin/bash
DIR=`pwd`
echo "BASH_MP3D - A Streaming MP3 Server in Bash "
echo "Generating Playlist .....Plz wait"
PLAYLIST=`find $DIR -name *.mp3`
#PLAYLIST=`ls *.mp3`
NUM=`echo $PLAYLIST | wc -w`
echo "done."
echo "Listening on 5700 ....."
{
while true
do
#get seconds for random number
rand=`date | cut -d ':' -f 3 |cut -d ' ' -f1`
let "rand += 1"
while [ "$rand" -gt "$NUM" ]
do
let "rand -= $NUM"
done
SONG=`echo $PLAYLIST | cut -d ' ' -f$rand`
#Now we play the random song SONG
echo "Playing $SONG ... " >& 2
echo "HTTP/1.0 200 OK";
echo "Content-Type: audio/x-mp3stream";
echo "Cache-Control: no-cache ";
echo "Pragma: no-cache ";
echo "Connection: close ";
echo "x-audiocast-name: BASH MP3 Server";
echo;
echo;
dd if=$SONG bs=1024
done
}| nc -l -p 5700
_________________________________________________________________
Is your PC infected? Get a FREE online computer virus scan from McAfee®
Security. http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
More information about the Discuss
mailing list