Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

can't write to file from socket

by femerrill (Initiate)
on Aug 04, 2014 at 15:16 UTC ( [id://1096153]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1096153]
Front-paged by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-18 01:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found