Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Comment on

#!/usr/bin/perl -w use IO::Socket; use strict; (@ARGV == 1 || @ARGV == 2) || die "usage: $0 port [filename]\n"; my ($port, $filename) = @ARGV; my ($buffersize, $bytesread, $data, $remote, $hostinfo, $size) = (1024 +, 0); my $server = IO::Socket::INET->new( Listen => SOMAXCONN, LocalPort => $port, Reuse => 1, Proto => 'tcp' ) || die "can't open connection: $!"; while (defined($remote = $server->accept)) { $remote->autoflush(1); $hostinfo = gethostbyaddr($remote->peeraddr, AF_INET); print STDERR "[Received connection from ", $hostinfo || $remote->p +eerhost," on $port]\n"; $data = <$remote>; ($data =~ /^\[FILENAME: "(.+?\.*?)"\]/) + ? $filename = $1 : die "incorrect header format: missing filename\n"; $data = <$remote>; ($data =~ /^\[SIZE: "(\d*)"\]/) || die "incorrect header format: f +ile size not indicated:$!"; $size = $1; (-e $filename) ? (-w $filename) || die "connot replace file: $filename:$!" : (-w ".") || die "cannot write to working directory\n"; open (FILE, ">$filename") || die "can't open output file $filename +:$!"; binmode FILE; print STDERR "[Retrieving $filename \($size bytes\)]\n"; while(read($remote, $data, $buffersize)) { print FILE $data; $bytesread += length($data); } close(FILE) || die "can't close output file: $filename:$!"; chmod 0555, $filename || die "cannot make output file readable:$!" +; print STDERR "[Finished receiving $filename, $bytesread bytes read + of $size expected]\n"; } close($server) || die "can't close network connection on port $port:$! +";

In reply to Custom file server by Aighearach

and:  <code> code here </code>

  • Please read these before you post! —
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    As of 2014-12-27 18:46 GMT
    Find Nodes?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?

      Results (177 votes), past polls