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}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.