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 | |
Re: Custom file server
by jano (Initiate) on Nov 15, 2002 at 19:34 UTC | |
by Aighearach (Initiate) on Nov 04, 2004 at 12:46 UTC |
Back to
Code Catacombs