Net::BitTorrent - BitTorrent peer-to-peer protocol class


  use Net::BitTorrent;

  my $client = Net::BitTorrent->new();

  # ...
  # Set various callbacks if you so desire
  # ...
      sub {
          my ($self, $args) = @_;
          printf(qq[pass: piece number %04d of %s\n],
                 $args->{q[Index]}, $args->{q[Torrent]}->infohash);
      sub {
          my ($self, $args) = @_;
          printf(qq[fail: piece number %04d of %s\n],
                 $args->{q[Index]}, $args->{q[Torrent]}->infohash);

  my $torrent = $client->add_torrent({Path => q[]})
      or die q[Cannot load .torrent];

  $torrent->hashcheck;    # Verify any existing data

  while (1) { $client->do_one_loop(); }


Net::BitTorrent is a class based implementation of the BitTorrent Protocol for distributed data exchange.


new ( { [ARGS] } )

Creates a Net::BitTorrent object. new ( ) accepts arguments as a hash, using key-value pairs, all of which are optional. The most common are:


Local host bind address. The value must be an IPv4 ("dotted quad") IP- address of the form.

Default: (any address)


TCP port opened to remote peers for incoming connections. If handed a list of ports, Net::BitTorrent will traverse the list, attempting to open on each of the ports until we succeed. If this value is undef or 0, we allow the OS to choose an open port at random.

Though the default in most clients is a random port in the 6881..6889 range, BitTorrent has not been assigned a port number or range by the IANA. Nor is such a standard needed.

Default: 0 (any available)


Unless otherwise stated, all methods return either a true or false value, with true meaning that the operation was a success. When a method states that it returns some other specific value, failure will result in undef or an empty list.

peerid ( )

Returns the Peer ID generated to identify this Net::BitTorrent object internally, with trackers, and with remote peers.

See also: (, Peer ID Specification

torrents( )

Returns the list of loaded .torrent Torrents.

See also: add_torrent ( ), remove_torrent ( )

add_torrent ( { ... } )

Loads a .torrent file and adds the new Net::BitTorrent::Torrent object to the client.

Most arguments passed to this method are handed directly to Net::BitTorrent::Torrent::new( ). The only mandatory parameter is Path. Path's value is the filename of the .torrent file to load. Please see Net::BitTorrent::Torrent::new( ) for a list of possible parameters.

This method returns the new Net::BitTorrent::Torrent object on success.

See also: torrents, remove_torrent, Net::BitTorrent::Torrent

remove_torrent ( TORRENT )

Removes a Net::BitTorrent::Torrent object from the client.

See also: torrents ( ), add_torrent, Net::BitTorrent::Torrent

do_one_loop ( [TIMEOUT] )

Processes the various socket-containing objects (peers, trackers) held by this Net::BitTorrent object. This method should be called frequently.

The optional TIMEOUT parameter is the maximum amount of time, in seconds, possibly fractional, select() is allowed to wait before returning in do_one_loop( ). This TIMEOUT defaults to 1.0. To wait indefinatly, TIMEOUT should be -1.0.

on_event ( TYPE, CODEREF )

Net::BitTorrent provides a convenient callback system. To set a callback, use the on_event( ) method. For example, to catch all attempts to read from a file, use $client-on_event( 'file_read', \&on_read )>.

See the Events section for a list of events sorted by their related classes.


Net::BitTorrent and related classes trigger a number of events



Sanko Robinson <> -


License and Legal

Copyright (C) 2008 by Sanko Robinson <>

This program is free software; you can redistribute it and/or modify it under the terms of The Artistic License 2.0. See the LICENSE file included with this distribution or For clarification, see

When separated from the distribution, all POD documentation is covered by the Creative Commons Attribution-Share Alike 3.0 License. See For clarification, see

Neither this module nor the Author is affiliated with BitTorrent, Inc.