http://www.perlmonks.org?node_id=804215

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