Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
P is for Practical
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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


In reply to Re: P2P Golf: MoleSter by bgreenlee
in thread P2P Golf: MoleSter by hossman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (4)
    As of 2014-04-20 21:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (487 votes), past polls