<?xml version="1.0" encoding="windows-1252"?>
<node id="9313" title="Custom file server" created="2000-04-26 19:57:23" updated="2005-08-11 07:55:30">
<type id="1748">
sourcecode</type>
<author id="8329">
Aighearach</author>
<data>
<field name="doctext">
&lt;CODE&gt;
#!/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-&gt;new(
				   Listen    =&gt; SOMAXCONN,
				   LocalPort =&gt; $port,    
				   Reuse     =&gt; 1,        
				   Proto     =&gt; 'tcp' )   
    || die "can't open connection: $!";                   
while (defined($remote = $server-&gt;accept)) {              
    $remote-&gt;autoflush(1);                                
    $hostinfo = gethostbyaddr($remote-&gt;peeraddr, AF_INET);
    print STDERR "[Received connection from ", $hostinfo || $remote-&gt;peerhost," on $port]\n";
    $data = &lt;$remote&gt;;
    ($data =~ /^\[FILENAME: "(.+?\.*?)"\]/)                                                        
	? $filename = $1
	    : die "incorrect header format: missing filename\n";
    $data = &lt;$remote&gt;;
    ($data =~ /^\[SIZE: "(\d*)"\]/) || die "incorrect header format: file size not indicated:$!";
    $size = $1;
    (-e $filename)
	? (-w $filename) || die "connot replace file: $filename:$!" 
	    : (-w ".") || die "cannot write to working directory\n";
    open (FILE, "&gt;$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:$!"; 
&lt;/CODE&gt;</field>
<field name="codedescription">
This is a basic file server program. It was written as an example, to be included in a bot on the &lt;A HREF=http://www.freechess.org&gt;free internet chess server&lt;/A&gt;.
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.</field>
<field name="codecategory">
Network</field>
<field name="codeauthor">
Paris Sinclair
&lt;A HREF="mailto:paris@perlpuma.net"&gt;paris@perlpuma.net&lt;/a&gt;</field>
</data>
</node>
