femerrill has asked for the wisdom of the Perl Monks concerning the following question:

Listening on port 50000 (Avaya CDR), the script creates the output file, receives the data stream on port 50000, prints fine to the screen, but does not write to the output file. What am I missing?

use IO::Socket; use POSIX; $port = 50000; for(;;){ my $sock = IO::Socket::INET->new( Listen => 5, LocalPort => $port, Proto => 'tcp' ) or die "Can't create server socket: $!"; my $client = $sock->accept; open FILE, ">output" or die "Can't open: $!"; while (<$client>) { s/^\0+//; # Remove leading null characters print $_; print FILE $_; } #close the socket close $sock or die "close: $!"; close FILE; }

Replies are listed 'Best First'.
Re: can't write to file from socket
by GotToBTru (Prior) on Aug 04, 2014 at 15:32 UTC

    I wonder if you are constantly overwriting your output file. Perhaps change to:

    OPEN FILE, ">>output" or die "Can't open: $!";
    1 Peter 4:10

      Looks like you may have the solution! Thanks.

Re: can't write to file from socket (flush)
by tye (Sage) on Aug 04, 2014 at 16:42 UTC

    Google "suffering from buffering".

    Update: Ah, I see the close is inside of the loop, so this shouldn't be the source of the problem.

    - tye        

      Buffering was definitely an issue. I think I now have a solution. Thanks.

Re: can't write to file from socket
by Anonymous Monk on Aug 05, 2014 at 12:55 UTC
    When you are certain that you do, please briefly describe it here.