Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: P2P Golf: MoleSter

by bgreenlee (Friar)
on Dec 16, 2004 at 16:30 UTC ( [id://415397]=note: print w/replies, xml ) Need Help??


in reply to P2P Golf: MoleSter

I missed this post yesterday, as I was deep into writing my own perl P2P app, after someone posted the challenge to the Fun With Perl mailing list. Mine is slightly bigger (717 characters, including newlines), but still 9 lines. I worked under the assumption that any modules in the standard perl distro were fair game (hence IO::Socket)

I tried to emulate the operation of the original python version as closely as possible. I did add one feature that i thought was essential, though: the ability to view files on the network without actually downloading them.

I'm sure better golfers than I could knock this down a bit.

Anyway, mine is used like so:

Server:
perl tinyp2p.pl server <passwd> <listenhost:listenport> [<peerhost:peerport>]

Client:
perl tinyp2p.pl client <passwd> <serverhost:serverport> list|get <pattern>

Note that when entering a pattern, you need to escape the backslash. So to search for .mp3 files, the pattern would be \\.mp3$

use IO::Socket;($t,$s,$u,$p,$x)=@ARGV;if($t eq"server"){$p&&($p{$p}++, +map{$_ ne $u&&$p{$_}++}f($p,2,$u));while($c=new IO::Socket::INET(LocalAddr=>$u,R +euse=>1, Listen=>9)->accept()){if(<$c>eq"$s\n"){($r=<$c>)>1?do{print$c "$_\n"fo +r keys%p; chomp($a=<$c>);$a&&$p{$a}++;}:do{chomp($x=<$c>);for(grep/$x/,<*>){$z=( +stat)[7]; print$c "$_/$z\n";if($r>0){open G,$_;read G,$b,$z;close;print$c $b}}}} +close$c}} for$v($u,f($u,2,0)){printf"%s (%s)\n",@$_ for f($v,$p eq"get",$x)}sub +f{($h,$q, $m)=@_;$k=new IO::Socket::INET($h)||return;print$k "$s\n$q\n$m",define +d$m?"\n": '';$q>1?chomp(@r=<$k>):do{@r=();while(<$k>){chomp;($f,$e)=split'/';pus +h@r,[$f,$e ]if!$q|!-e$f;next if!$q|-e$f;read$k,$b,$e;open F,">$f";print F$b;close +}};@r}

-b

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://415397]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-19 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found