Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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


In reply to Opens but Won't Write to file by saintjames

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (10)
    As of 2014-11-26 01:41 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (160 votes), past polls