1
<?xml version="1.0" encoding="utf-8" ?>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
<meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
7
<title>libtorrent Examples</title>
8
<meta name="author" content="Arvid Norberg, arvid@rasterbar.com" />
9
<link rel="stylesheet" type="text/css" href="../../css/base.css" />
10
<link rel="stylesheet" type="text/css" href="../../css/rst.css" />
11
<link rel="stylesheet" href="style.css" type="text/css" />
12
<style type="text/css">
13
/* Hides from IE-mac \*/
14
* html pre { height: 1%; }
15
/* End hide from IE-mac */
19
<div class="document" id="libtorrent-examples">
23
<li class="first"><a href="/">Home</a></li>
24
<li><a href="../../products.html">Products</a></li>
25
<li><a href="../../contact.html">Contact</a></li>
29
<h1><span>Rasterbar Software</span></h1>
30
<h2><span>Software developement and consulting</span></h2>
33
<h1 class="title">libtorrent Examples</h1>
34
<table class="docinfo" frame="void" rules="none">
35
<col class="docinfo-name" />
36
<col class="docinfo-content" />
38
<tr><th class="docinfo-name">Author:</th>
39
<td>Arvid Norberg, <a class="last reference external" href="mailto:arvid@rasterbar.com">arvid@rasterbar.com</a></td></tr>
42
<div class="contents topic" id="table-of-contents">
43
<p class="topic-title first">Table of contents</p>
45
<li><a class="reference internal" href="#examples" id="id2">examples</a><ul>
46
<li><a class="reference internal" href="#dump-torrent" id="id3">dump_torrent</a></li>
47
<li><a class="reference internal" href="#simple-client" id="id4">simple client</a></li>
48
<li><a class="reference internal" href="#make-torrent" id="id5">make_torrent</a></li>
53
<div class="section" id="examples">
55
<p>Except for the example programs in this manual, there's also a bigger example
56
of a (little bit) more complete client, <tt class="docutils literal"><span class="pre">client_test</span></tt>. There are separate
57
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
58
<tt class="docutils literal"><span class="pre">client_test</span></tt> also requires boost.regex and boost.program_options library.</p>
59
<div class="section" id="dump-torrent">
61
<p>This is an example of a program that will take a torrent-file as a parameter and
62
print information about it to std out:</p>
63
<pre class="literal-block">
64
#include <iostream>
65
#include <fstream>
66
#include <iterator>
67
#include <iomanip>
69
#include "libtorrent/entry.hpp"
70
#include "libtorrent/bencode.hpp"
71
#include "libtorrent/torrent_info.hpp"
72
#include "libtorrent/lazy_entry.hpp"
73
#include <boost/filesystem/operations.hpp>
76
int main(int argc, char* argv[])
78
using namespace libtorrent;
79
using namespace boost::filesystem;
83
std::cerr << "usage: dump_torrent torrent-file\n";
86
#if BOOST_VERSION < 103400
87
boost::filesystem::path::default_name_check(boost::filesystem::no_check);
90
#ifndef BOOST_NO_EXCEPTIONS
95
int size = file_size(argv[1]);
96
if (size > 10 * 1000000)
98
std::cerr << "file too big (" << size << "), aborting\n";
101
std::vector<char> buf(size);
102
std::ifstream(argv[1], std::ios_base::binary).read(&buf[0], size);
104
int ret = lazy_bdecode(&buf[0], &buf[0] + buf.size(), e);
108
std::cerr << "invalid bencoding: " << ret << std::endl;
112
std::cout << "\n\n----- raw info -----\n\n";
113
std::cout << e << std::endl;
117
// print info about torrent
118
std::cout << "\n\n----- torrent file info -----\n\n";
119
std::cout << "nodes:\n";
120
typedef std::vector<std::pair<std::string, int> > node_vec;
121
node_vec const& nodes = t.nodes();
122
for (node_vec::const_iterator i = nodes.begin(), end(nodes.end());
125
std::cout << i->first << ":" << i->second << "\n";
127
std::cout << "trackers:\n";
128
for (std::vector<announce_entry>::const_iterator i = t.trackers().begin();
129
i != t.trackers().end(); ++i)
131
std::cout << i->tier << ": " << i->url << "\n";
134
std::cout << "number of pieces: " << t.num_pieces() << "\n";
135
std::cout << "piece length: " << t.piece_length() << "\n";
136
std::cout << "info hash: " << t.info_hash() << "\n";
137
std::cout << "comment: " << t.comment() << "\n";
138
std::cout << "created by: " << t.creator() << "\n";
139
std::cout << "files:\n";
141
for (torrent_info::file_iterator i = t.begin_files();
142
i != t.end_files(); ++i, ++index)
144
int first = t.map_file(index, 0, 1).piece;
145
int last = t.map_file(index, i->size - 1, 1).piece;
146
std::cout << " " << std::setw(11) << i->size
147
<< " " << i->path.string() << "[ " << first << ", "
148
<< last << " ]\n";
151
#ifndef BOOST_NO_EXCEPTIONS
153
catch (std::exception& e)
155
std::cout << e.what() << "\n";
163
<div class="section" id="simple-client">
164
<h2>simple client</h2>
165
<p>This is a simple client. It doesn't have much output to keep it simple:</p>
166
<pre class="literal-block">
167
int main(int argc, char* argv[])
169
using namespace libtorrent;
170
#if BOOST_VERSION < 103400
171
namespace fs = boost::filesystem;
172
fs::path::default_name_check(fs::no_check);
177
std::cerr << "usage: ./simple_client torrent-file\n"
178
"to stop the client, press return.\n";
182
#ifndef BOOST_NO_EXCEPTIONS
187
s.listen_on(std::make_pair(6881, 6889));
188
add_torrent_params p;
189
p.save_path = "./";
190
p.ti = new torrent_info(argv[1]);
193
// wait for the user to end
195
std::cin.unsetf(std::ios_base::skipws);
198
#ifndef BOOST_NO_EXCEPTIONS
199
catch (std::exception& e)
201
std::cout << e.what() << "\n";
208
<div class="section" id="make-torrent">
209
<h2>make_torrent</h2>
210
<p>Shows how to create a torrent from a directory tree:</p>
211
<pre class="literal-block">
212
#include <iostream>
213
#include <fstream>
214
#include <iterator>
215
#include <iomanip>
217
#include "libtorrent/entry.hpp"
218
#include "libtorrent/bencode.hpp"
219
#include "libtorrent/torrent_info.hpp"
220
#include "libtorrent/file.hpp"
221
#include "libtorrent/storage.hpp"
222
#include "libtorrent/hasher.hpp"
223
#include "libtorrent/create_torrent.hpp"
225
#include <boost/filesystem/operations.hpp>
226
#include <boost/filesystem/path.hpp>
227
#include <boost/filesystem/fstream.hpp>
228
#include <boost/bind.hpp>
230
using namespace boost::filesystem;
231
using namespace libtorrent;
233
// do not include files and folders whose
234
// name starts with a .
235
bool file_filter(boost::filesystem::path const& filename)
237
if (filename.leaf()[0] == '.') return false;
238
std::cerr << filename << std::endl;
242
void print_progress(int i, int num)
244
std::cerr << "\r" << (i+1) << "/" << num;
247
int main(int argc, char* argv[])
249
using namespace libtorrent;
250
using namespace boost::filesystem;
252
int piece_size = 256 * 1024;
253
char const* creator_str = "libtorrent";
255
path::default_name_check(no_check);
257
if (argc != 4 && argc != 5)
259
std::cerr << "usage: make_torrent <output torrent-file> "
260
"<announce url> <file or directory to create torrent from> "
261
"[url-seed]\n";
265
#ifndef BOOST_NO_EXCEPTIONS
271
path full_path = complete(path(argv[3]));
273
add_files(fs, full_path, file_filter);
275
create_torrent t(fs, piece_size);
276
t.add_tracker(argv[2]);
277
set_piece_hashes(t, full_path.branch_path()
278
, boost::bind(&print_progress, _1, t.num_pieces()));
279
std::cerr << std::endl;
280
t.set_creator(creator_str);
282
if (argc == 5) t.add_url_seed(argv[4]);
284
// create the torrent and print it to out
285
ofstream out(complete(path(argv[1])), std::ios_base::binary);
286
bencode(std::ostream_iterator<char>(out), t.generate());
287
#ifndef BOOST_NO_EXCEPTIONS
289
catch (std::exception& e)
291
std::cerr << e.what() << "\n";
302
<span>Copyright © 2005 Rasterbar Software.</span>
305
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
307
<script type="text/javascript">
308
_uacct = "UA-1599045-1";