http://www.perlmonks.org?node_id=9313
Category: Network
Author/Contact Info Paris Sinclair paris@perlpuma.net
Description: This is a basic file server program. It was written as an example, to be included in a bot on the free internet chess server. Yes, there are modules that make this easy, but the intention was to have a custom, extensible protocol. Currently, it is very basic. In the future, it will be converted to a module, and will be able to identify/convert popular chess notation formats. It has been tested under linux, sunos, and windows. A companion client program will also be provided; check where you found this one.
#!/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:$!
+";
Replies are listed 'Best First'.
Re: Custom file server
by DarkSniper (Initiate) on Feb 02, 2004 at 15:16 UTC
    this code helped alot in understanding the basics of network code, thnx alot!
Re: Custom file server
by jano (Initiate) on Nov 15, 2002 at 19:34 UTC
    Hi: You know I have a question as you know your that the connection not hill before finishing envio of the message, exists a form to know when to the connection is broken?
      I've updated the code so that when it is done reading, it will print both the bytes read and also the expected bytes read. I doubt that is all that is needed to handle the actual cases, but it would at least cover the simple case where the client closes the connection. If you need it to be more robust, give it a whack and post what you have, and I'll help with any problems you have.

      --
      Snazzy tagline here