2006-08-28 02:36:00 +02:00
|
|
|
<?xml version="1.0" encoding="utf-8" ?>
|
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
|
|
<head>
|
2012-01-16 03:09:07 +01:00
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
2014-05-12 08:28:47 +02:00
|
|
|
<meta name="generator" content="Docutils 0.11: http://docutils.sourceforge.net/" />
|
2012-01-16 03:09:07 +01:00
|
|
|
<title>libtorrent Examples</title>
|
|
|
|
<meta name="author" content="Arvid Norberg, arvid@rasterbar.com" />
|
|
|
|
<link rel="stylesheet" type="text/css" href="../../css/base.css" />
|
|
|
|
<link rel="stylesheet" type="text/css" href="../../css/rst.css" />
|
2010-12-08 05:44:20 +01:00
|
|
|
<script type="text/javascript">
|
|
|
|
/* <![CDATA[ */
|
|
|
|
(function() {
|
|
|
|
var s = document.createElement('script'), t = document.getElementsByTagName('script')[0];
|
|
|
|
s.type = 'text/javascript';
|
|
|
|
s.async = true;
|
|
|
|
s.src = 'http://api.flattr.com/js/0.6/load.js?mode=auto';
|
|
|
|
t.parentNode.insertBefore(s, t);
|
|
|
|
})();
|
|
|
|
/* ]]> */
|
|
|
|
</script>
|
2006-08-28 02:36:00 +02:00
|
|
|
<link rel="stylesheet" href="style.css" type="text/css" />
|
2008-10-17 05:23:30 +02:00
|
|
|
<style type="text/css">
|
|
|
|
/* Hides from IE-mac \*/
|
|
|
|
* html pre { height: 1%; }
|
|
|
|
/* End hide from IE-mac */
|
|
|
|
</style>
|
2006-08-28 02:36:00 +02:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div class="document" id="libtorrent-examples">
|
2008-10-17 05:23:30 +02:00
|
|
|
<div id="container">
|
|
|
|
<div id="headerNav">
|
|
|
|
<ul>
|
|
|
|
<li class="first"><a href="/">Home</a></li>
|
|
|
|
<li><a href="../../products.html">Products</a></li>
|
|
|
|
<li><a href="../../contact.html">Contact</a></li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
<div id="header">
|
2013-08-04 11:02:19 +02:00
|
|
|
<div id="orange"></div>
|
|
|
|
<div id="logo"></div>
|
2008-10-17 05:23:30 +02:00
|
|
|
</div>
|
|
|
|
<div id="main">
|
2006-08-28 02:36:00 +02:00
|
|
|
<h1 class="title">libtorrent Examples</h1>
|
|
|
|
<table class="docinfo" frame="void" rules="none">
|
|
|
|
<col class="docinfo-name" />
|
|
|
|
<col class="docinfo-content" />
|
|
|
|
<tbody valign="top">
|
|
|
|
<tr><th class="docinfo-name">Author:</th>
|
2009-03-21 05:36:46 +01:00
|
|
|
<td>Arvid Norberg, <a class="last reference external" href="mailto:arvid@rasterbar.com">arvid@rasterbar.com</a></td></tr>
|
2006-08-28 02:36:00 +02:00
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
<div class="contents topic" id="table-of-contents">
|
2008-10-17 05:23:30 +02:00
|
|
|
<p class="topic-title first">Table of contents</p>
|
2006-08-28 02:36:00 +02:00
|
|
|
<ul class="simple">
|
2009-03-21 05:36:46 +01:00
|
|
|
<li><a class="reference internal" href="#examples" id="id2">examples</a><ul>
|
|
|
|
<li><a class="reference internal" href="#dump-torrent" id="id3">dump_torrent</a></li>
|
|
|
|
<li><a class="reference internal" href="#simple-client" id="id4">simple client</a></li>
|
|
|
|
<li><a class="reference internal" href="#make-torrent" id="id5">make_torrent</a></li>
|
2006-08-28 02:36:00 +02:00
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
2008-10-17 05:23:30 +02:00
|
|
|
<div class="section" id="examples">
|
|
|
|
<h1>examples</h1>
|
2006-08-28 02:36:00 +02:00
|
|
|
<p>Except for the example programs in this manual, there's also a bigger example
|
2013-01-21 20:13:24 +01:00
|
|
|
of a (little bit) more complete client, <tt class="docutils literal">client_test</tt>. There are separate
|
2009-03-21 05:36:46 +01:00
|
|
|
instructions for how to use it <a class="reference external" href="client_test.html">here</a> if you'd like to try it. Note that building
|
2013-01-21 20:13:24 +01:00
|
|
|
<tt class="docutils literal">client_test</tt> also requires boost.regex and boost.program_options library.</p>
|
2008-10-17 05:23:30 +02:00
|
|
|
<div class="section" id="dump-torrent">
|
|
|
|
<h2>dump_torrent</h2>
|
2006-08-28 02:36:00 +02:00
|
|
|
<p>This is an example of a program that will take a torrent-file as a parameter and
|
|
|
|
print information about it to std out:</p>
|
|
|
|
<pre class="literal-block">
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/torrent_info.hpp"
|
2008-08-09 09:34:12 +02:00
|
|
|
#include "libtorrent/lazy_entry.hpp"
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
2006-08-28 02:36:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
2008-08-09 09:34:12 +02:00
|
|
|
using namespace boost::filesystem;
|
2006-08-28 02:36:00 +02:00
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
{
|
|
|
|
std::cerr << "usage: dump_torrent torrent-file\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2008-08-09 09:34:12 +02:00
|
|
|
#if BOOST_VERSION < 103400
|
|
|
|
boost::filesystem::path::default_name_check(boost::filesystem::no_check);
|
|
|
|
#endif
|
2006-08-28 02:36:00 +02:00
|
|
|
|
2008-08-09 09:34:12 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2006-08-28 02:36:00 +02:00
|
|
|
try
|
|
|
|
{
|
2008-08-09 09:34:12 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
int size = file_size(argv[1]);
|
|
|
|
if (size > 10 * 1000000)
|
|
|
|
{
|
|
|
|
std::cerr << "file too big (" << size << "), aborting\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
std::vector<char> buf(size);
|
|
|
|
std::ifstream(argv[1], std::ios_base::binary).read(&buf[0], size);
|
|
|
|
lazy_entry e;
|
|
|
|
int ret = lazy_bdecode(&buf[0], &buf[0] + buf.size(), e);
|
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
std::cerr << "invalid bencoding: " << ret << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-08-28 02:36:00 +02:00
|
|
|
|
|
|
|
std::cout << "\n\n----- raw info -----\n\n";
|
2008-08-09 09:34:12 +02:00
|
|
|
std::cout << e << std::endl;
|
2006-08-28 02:36:00 +02:00
|
|
|
|
|
|
|
torrent_info t(e);
|
|
|
|
|
|
|
|
// print info about torrent
|
|
|
|
std::cout << "\n\n----- torrent file info -----\n\n";
|
2006-09-03 21:53:19 +02:00
|
|
|
std::cout << "nodes:\n";
|
|
|
|
typedef std::vector<std::pair<std::string, int> > node_vec;
|
|
|
|
node_vec const& nodes = t.nodes();
|
|
|
|
for (node_vec::const_iterator i = nodes.begin(), end(nodes.end());
|
|
|
|
i != end; ++i)
|
|
|
|
{
|
|
|
|
std::cout << i->first << ":" << i->second << "\n";
|
|
|
|
}
|
2006-08-28 02:36:00 +02:00
|
|
|
std::cout << "trackers:\n";
|
2008-08-09 09:34:12 +02:00
|
|
|
for (std::vector<announce_entry>::const_iterator i = t.trackers().begin();
|
|
|
|
i != t.trackers().end(); ++i)
|
2006-08-28 02:36:00 +02:00
|
|
|
{
|
|
|
|
std::cout << i->tier << ": " << i->url << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "number of pieces: " << t.num_pieces() << "\n";
|
|
|
|
std::cout << "piece length: " << t.piece_length() << "\n";
|
|
|
|
std::cout << "info hash: " << t.info_hash() << "\n";
|
|
|
|
std::cout << "comment: " << t.comment() << "\n";
|
|
|
|
std::cout << "created by: " << t.creator() << "\n";
|
|
|
|
std::cout << "files:\n";
|
2008-08-09 09:34:12 +02:00
|
|
|
int index = 0;
|
2006-08-28 02:36:00 +02:00
|
|
|
for (torrent_info::file_iterator i = t.begin_files();
|
2008-08-09 09:34:12 +02:00
|
|
|
i != t.end_files(); ++i, ++index)
|
2006-08-28 02:36:00 +02:00
|
|
|
{
|
2008-08-09 09:34:12 +02:00
|
|
|
int first = t.map_file(index, 0, 1).piece;
|
|
|
|
int last = t.map_file(index, i->size - 1, 1).piece;
|
2006-08-28 02:36:00 +02:00
|
|
|
std::cout << " " << std::setw(11) << i->size
|
2008-08-09 09:34:12 +02:00
|
|
|
<< " " << i->path.string() << "[ " << first << ", "
|
|
|
|
<< last << " ]\n";
|
2006-08-28 02:36:00 +02:00
|
|
|
}
|
2006-11-28 19:18:37 +01:00
|
|
|
|
2008-08-09 09:34:12 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2006-08-28 02:36:00 +02:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
std::cout << e.what() << "\n";
|
|
|
|
}
|
2008-08-09 09:34:12 +02:00
|
|
|
#endif
|
2006-08-28 02:36:00 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
</pre>
|
|
|
|
</div>
|
2008-10-17 05:23:30 +02:00
|
|
|
<div class="section" id="simple-client">
|
|
|
|
<h2>simple client</h2>
|
2006-08-28 02:36:00 +02:00
|
|
|
<p>This is a simple client. It doesn't have much output to keep it simple:</p>
|
|
|
|
<pre class="literal-block">
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (argc != 2)
|
|
|
|
{
|
|
|
|
fputs("usage: ./simple_client torrent-file\n"
|
|
|
|
"to stop the client, press return.\n", stderr);
|
|
|
|
return 1;
|
|
|
|
}
|
2006-08-28 02:36:00 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
session s;
|
|
|
|
error_code ec;
|
|
|
|
s.listen_on(std::make_pair(6881, 6889), ec);
|
|
|
|
if (ec)
|
2006-08-28 02:36:00 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
fprintf(stderr, "failed to open listen socket: %s\n", ec.message().c_str());
|
|
|
|
return 1;
|
2006-08-28 02:36:00 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
add_torrent_params p;
|
|
|
|
p.save_path = "./";
|
|
|
|
p.ti = boost::make_shared<torrent_info>(argv[1], ec);
|
|
|
|
if (ec)
|
2006-08-28 02:36:00 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
fprintf(stderr, "%s\n", ec.message().c_str());
|
|
|
|
return 1;
|
2006-08-28 02:36:00 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
s.add_torrent(p, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s\n", ec.message().c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for the user to end
|
|
|
|
char a;
|
|
|
|
scanf("%c\n", &a);
|
2006-08-28 02:36:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
</pre>
|
|
|
|
</div>
|
2008-10-17 05:23:30 +02:00
|
|
|
<div class="section" id="make-torrent">
|
|
|
|
<h2>make_torrent</h2>
|
2006-08-28 02:36:00 +02:00
|
|
|
<p>Shows how to create a torrent from a directory tree:</p>
|
|
|
|
<pre class="literal-block">
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/torrent_info.hpp"
|
|
|
|
#include "libtorrent/file.hpp"
|
|
|
|
#include "libtorrent/storage.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
2008-08-09 09:34:12 +02:00
|
|
|
#include "libtorrent/create_torrent.hpp"
|
2006-08-28 02:36:00 +02:00
|
|
|
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
#include <boost/filesystem/fstream.hpp>
|
2008-08-09 09:34:12 +02:00
|
|
|
#include <boost/bind.hpp>
|
2006-08-28 02:36:00 +02:00
|
|
|
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2008-08-09 09:34:12 +02:00
|
|
|
// do not include files and folders whose
|
|
|
|
// name starts with a .
|
|
|
|
bool file_filter(boost::filesystem::path const& filename)
|
2006-08-28 02:36:00 +02:00
|
|
|
{
|
2008-08-09 09:34:12 +02:00
|
|
|
if (filename.leaf()[0] == '.') return false;
|
|
|
|
std::cerr << filename << std::endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_progress(int i, int num)
|
|
|
|
{
|
|
|
|
std::cerr << "\r" << (i+1) << "/" << num;
|
2006-08-28 02:36:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
|
2008-08-09 09:34:12 +02:00
|
|
|
int piece_size = 256 * 1024;
|
|
|
|
char const* creator_str = "libtorrent";
|
|
|
|
|
2006-11-14 16:53:38 +01:00
|
|
|
path::default_name_check(no_check);
|
|
|
|
|
2008-08-09 09:34:12 +02:00
|
|
|
if (argc != 4 && argc != 5)
|
2006-08-28 02:36:00 +02:00
|
|
|
{
|
|
|
|
std::cerr << "usage: make_torrent <output torrent-file> "
|
2008-08-09 09:34:12 +02:00
|
|
|
"<announce url> <file or directory to create torrent from> "
|
|
|
|
"[url-seed]\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2006-08-28 02:36:00 +02:00
|
|
|
|
2008-08-09 09:34:12 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2006-08-28 02:36:00 +02:00
|
|
|
try
|
|
|
|
{
|
2008-08-09 09:34:12 +02:00
|
|
|
#endif
|
|
|
|
file_storage fs;
|
|
|
|
file_pool fp;
|
2006-11-14 16:53:38 +01:00
|
|
|
path full_path = complete(path(argv[3]));
|
2006-08-28 02:36:00 +02:00
|
|
|
|
2008-08-09 09:34:12 +02:00
|
|
|
add_files(fs, full_path, file_filter);
|
2006-08-28 02:36:00 +02:00
|
|
|
|
2008-08-09 09:34:12 +02:00
|
|
|
create_torrent t(fs, piece_size);
|
2006-08-28 02:36:00 +02:00
|
|
|
t.add_tracker(argv[2]);
|
2008-08-09 09:34:12 +02:00
|
|
|
set_piece_hashes(t, full_path.branch_path()
|
|
|
|
, boost::bind(&print_progress, _1, t.num_pieces()));
|
|
|
|
std::cerr << std::endl;
|
2006-08-28 02:36:00 +02:00
|
|
|
t.set_creator(creator_str);
|
|
|
|
|
2008-08-09 09:34:12 +02:00
|
|
|
if (argc == 5) t.add_url_seed(argv[4]);
|
|
|
|
|
2006-08-28 02:36:00 +02:00
|
|
|
// create the torrent and print it to out
|
2008-08-09 09:34:12 +02:00
|
|
|
ofstream out(complete(path(argv[1])), std::ios_base::binary);
|
|
|
|
bencode(std::ostream_iterator<char>(out), t.generate());
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
2006-08-28 02:36:00 +02:00
|
|
|
{
|
|
|
|
std::cerr << e.what() << "\n";
|
|
|
|
}
|
2008-08-09 09:34:12 +02:00
|
|
|
#endif
|
2006-08-28 02:36:00 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
</pre>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|