saintjames has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to set up a UDP server which listens to port and writes data received to file. The program opens the file however it won't write to it? I know that it is receiving data because for debugging I have an additional print statement that prints the data received to the terminal. Code follows:
#!/usr/local/bin/perl -w use strict; $|=1; use IO::Socket::INET; if (scalar @ARGV < 2) { die usage(); } my($filenam,$portnum,$OUTDIR,$OUTFIL,$outfil,$begin,$MySocket,$text); my($YEAR,$MO,$DAY,$HOUR,$MIN,$MONTH,$yr,$timeofday,$damoyr); $filenam = $ARGV[0]; $portnum = $ARGV[1]; my(@NOW) = localtime(); ($YEAR,$MO,$DAY,$HOUR,$MIN) = @NOW[5,4,3,2,1]; $MONTH = $MO + 1; $YEAR = $YEAR + 1900; $yr = $YEAR - 2000; $damoyr = sprintf("%02d/%02d/%02d",$MONTH,$DAY,$yr); $timeofday = sprintf("%02d:%02d",$HOUR,$MIN); $OUTDIR = "/active/"; $outfil = $OUTDIR . $filenam; print "outfile is $outfil\n"; open(OUTFIL,">$outfil"); # Create a new socket $MySocket=new IO::Socket::INET->new(LocalPort=>$portnum, Proto=>'udp'); $begin="\nStarting UDP Receiver $damoyr $timeofday\n"; print OUTFIL "$begin"; # Keep receiving messages; while(1) { $MySocket->recv($text,128); print OUTFIL "$text#$damoyr $timeofday\n"; print "$text#$damoyr $timeofday\n"; } close(OUTFIL); sub usage { my $usage = <<EOF; Usage: $0 <filename> <portnum> - filename is name of log file in the logging directory - portnum is the UDP port number to listen to EOF return $usage; }
I see the output file newly opened each time I run this and I use a simple udpclient to send to the same port that this listens to and can see the output printed to the terminal but never the file. Any wisdom would be graciously accepted, thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Opens but Won't Write to file
by Joost (Canon) on Oct 30, 2009 at 20:58 UTC | |
by Bloodnok (Vicar) on Oct 30, 2009 at 23:09 UTC | |
by Joost (Canon) on Oct 30, 2009 at 23:20 UTC | |
|
Re: Opens but Won't Write to file
by MidLifeXis (Monsignor) on Oct 30, 2009 at 21:30 UTC | |
by ikegami (Patriarch) on Oct 30, 2009 at 21:49 UTC | |
by BrowserUk (Patriarch) on Oct 30, 2009 at 22:08 UTC | |
by ikegami (Patriarch) on Oct 30, 2009 at 22:16 UTC | |
by chromatic (Archbishop) on Oct 31, 2009 at 08:58 UTC | |
by saintjames (Novice) on Oct 30, 2009 at 22:36 UTC | |
by MidLifeXis (Monsignor) on Nov 01, 2009 at 00:21 UTC |