char1es.net

dropd.pl

October 9th, 2007

Mac using Anders wanted to dump something on my laptop running Ubuntu. He suggested dropcopy but there wasn’t a server for linux. Turns out the drop copy protocol is pretty basic and it wasn’t too difficult to write a server in perl. This could be integrated into avahi so you can use bonjour. Code follows,


#!/usr/bin/perl
#Author: Nathan Charles <ncharles at gmail dot com>
#
#This is a quick and dirty DropCopy server for linux written in perl.
#It dumps the file into the directory the sever is run from.
#It works for me. Your mileage may vary.
#

use IO::Socket;
print “Starting Server\n”;

my $SERVER_RUNNING = TRUE;

while($SERVER_RUNNING) {
my $sock = new IO::Socket::INET (
LocalHost => ‘localhost’,
LocalPort => ’5052′,
Proto => ‘tcp’,
Reuse => 1,
);
die “Could not create socket: $!\n” unless $sock;

$sock->listen();
$sock->autoflush(1);
my $new_sock = $sock->accept();
my $HEADER_FLAG = “1″;
my $header;
my $filename;
my $filesize;
@metadata;
$metacounter=0;
$filesize=0;
my $trans=0;

while(<$new_sock>) {
if ($HEADER_FLAG) {
$HEADER_FLAG=0;
$headerlen= length($_);
$header = substr($_,0,$headerlen-1);
print $header . “\n”;
$headerlen= length($_);
@metadata = split(/\|/,$header);
$filename = @metadata[1];
$filesize = @metadata[2];
print “1=”.@metadata[1].” 2=”.@metadata[2].”\n”;
$_ = substr($_,$headerlen);
open(FILEOUT, “>”.$filename);
}
$trans +=length($_);
print FILEOUT $_;
if ($trans==$filesize) {
print $trans.” Bytes of “.$filesize.” transfered.\n”;
close(FILEOUT);
break;
}
}
close(FILEOUT);
close($sock);
}

- ❦ -