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" href=
"style.css" type=
"text/css" />
12 <div class=
"document" id=
"libtorrent-examples">
13 <h1 class=
"title">libtorrent Examples
</h1>
14 <table class=
"docinfo" frame=
"void" rules=
"none">
15 <col class=
"docinfo-name" />
16 <col class=
"docinfo-content" />
18 <tr><th class=
"docinfo-name">Author:
</th>
19 <td>Arvid Norberg,
<a class=
"last reference" href=
"mailto:arvid@rasterbar.com">arvid
@rasterbar.com
</a></td></tr>
22 <div class=
"contents topic" id=
"table-of-contents">
23 <p class=
"topic-title first"><a name=
"table-of-contents">Table of contents
</a></p>
25 <li><a class=
"reference" href=
"#examples" id=
"id2" name=
"id2">examples
</a><ul>
26 <li><a class=
"reference" href=
"#dump-torrent" id=
"id3" name=
"id3">dump_torrent
</a></li>
27 <li><a class=
"reference" href=
"#simple-client" id=
"id4" name=
"id4">simple client
</a></li>
28 <li><a class=
"reference" href=
"#make-torrent" id=
"id5" name=
"id5">make_torrent
</a></li>
34 <h1><a id=
"examples" name=
"examples">examples
</a></h1>
35 <p>Except for the example programs in this manual, there's also a bigger example
36 of a (little bit) more complete client,
<tt class=
"docutils literal"><span class=
"pre">client_test
</span></tt>. There are separate
37 instructions for how to use it
<a class=
"reference" href=
"client_test.html">here
</a> if you'd like to try it. Note that building
38 <tt class=
"docutils literal"><span class=
"pre">client_test
</span></tt> also requires boost.regex and boost.program_options library.
</p>
40 <h2><a id=
"dump-torrent" name=
"dump-torrent">dump_torrent
</a></h2>
41 <p>This is an example of a program that will take a torrent-file as a parameter and
42 print information about it to std out:
</p>
43 <pre class=
"literal-block">
44 #include
<iostream
>
45 #include
<fstream
>
46 #include
<iterator
>
47 #include
<iomanip
>
49 #include
"libtorrent/entry.hpp
"
50 #include
"libtorrent/bencode.hpp
"
51 #include
"libtorrent/torrent_info.hpp
"
54 int main(int argc, char* argv[])
56 using namespace libtorrent;
60 std::cerr
<< "usage: dump_torrent torrent-file\n
";
66 std::ifstream in(argv[
1], std::ios_base::binary);
67 in.unsetf(std::ios_base::skipws);
68 entry e = bdecode(std::istream_iterator
<char
>(in)
69 , std::istream_iterator
<char
>());
71 std::cout
<< "\n\n----- raw info -----\n\n
";
76 // print info about torrent
77 std::cout
<< "\n\n----- torrent file info -----\n\n
";
78 std::cout
<< "nodes:\n
";
79 typedef std::vector
<std::pair
<std::string, int
> > node_vec;
80 node_vec const
& nodes = t.nodes();
81 for (node_vec::const_iterator i = nodes.begin(), end(nodes.end());
84 std::cout
<< i-
>first
<< ":
" << i-
>second
<< "\n
";
87 std::cout
<< "trackers:\n
";
88 for (std::vector
<announce_entry
>::const_iterator i
89 = t.trackers().begin(); i != t.trackers().end(); ++i)
91 std::cout
<< i-
>tier
<< ":
" << i-
>url
<< "\n
";
94 std::cout
<< "number of pieces:
" << t.num_pieces()
<< "\n
";
95 std::cout
<< "piece length:
" << t.piece_length()
<< "\n
";
96 std::cout
<< "info hash:
" << t.info_hash()
<< "\n
";
97 std::cout
<< "comment:
" << t.comment()
<< "\n
";
98 std::cout
<< "created by:
" << t.creator()
<< "\n
";
99 std::cout
<< "files:\n
";
100 for (torrent_info::file_iterator i = t.begin_files();
101 i != t.end_files(); ++i)
103 std::cout
<< " " << std::setw(
11)
<< i-
>size
104 << " " << i-
>path.string()
<< "\n
";
108 catch (std::exception
& e)
110 std::cout
<< e.what()
<< "\n
";
117 <div class=
"section">
118 <h2><a id=
"simple-client" name=
"simple-client">simple client
</a></h2>
119 <p>This is a simple client. It doesn't have much output to keep it simple:
</p>
120 <pre class=
"literal-block">
121 #include
<iostream
>
122 #include
<fstream
>
123 #include
<iterator
>
124 #include
<exception
>
126 #include
<boost/format.hpp
>
127 #include
<boost/date_time/posix_time/posix_time.hpp
>
129 #include
"libtorrent/entry.hpp
"
130 #include
"libtorrent/bencode.hpp
"
131 #include
"libtorrent/session.hpp
"
133 int main(int argc, char* argv[])
135 using namespace libtorrent;
139 std::cerr
<< "usage: ./simple_cient torrent-file\n
"
140 "to stop the client, press return.\n
";
147 s.listen_on(std::make_pair(
6881,
6889));
149 std::ifstream in(argv[
1], std::ios_base::binary);
150 in.unsetf(std::ios_base::skipws);
151 entry e = bdecode(std::istream_iterator
<char
>(in)
152 , std::istream_iterator
<char
>());
153 s.add_torrent(torrent_info(e),
"");
155 // wait for the user to end
157 std::cin.unsetf(std::ios_base::skipws);
160 catch (std::exception
& e)
162 std::cout
<< e.what()
<< "\n
";
168 <div class=
"section">
169 <h2><a id=
"make-torrent" name=
"make-torrent">make_torrent
</a></h2>
170 <p>Shows how to create a torrent from a directory tree:
</p>
171 <pre class=
"literal-block">
172 #include
<iostream
>
173 #include
<fstream
>
174 #include
<iterator
>
175 #include
<iomanip
>
177 #include
"libtorrent/entry.hpp
"
178 #include
"libtorrent/bencode.hpp
"
179 #include
"libtorrent/torrent_info.hpp
"
180 #include
"libtorrent/file.hpp
"
181 #include
"libtorrent/storage.hpp
"
182 #include
"libtorrent/hasher.hpp
"
183 #include
"libtorrent/file_pool.hpp
"
185 #include
<boost/filesystem/operations.hpp
>
186 #include
<boost/filesystem/path.hpp
>
187 #include
<boost/filesystem/fstream.hpp
>
189 using namespace boost::filesystem;
190 using namespace libtorrent;
200 for (directory_iterator i(f), end; i != end; ++i)
201 add_files(t, p, l / i-
>leaf());
205 std::cerr
<< "adding \
"" << l.string()
<< "\
"\n
";
206 t.add_file(l, file_size(f));
210 int main(int argc, char* argv[])
212 using namespace libtorrent;
213 using namespace boost::filesystem;
215 path::default_name_check(no_check);
219 std::cerr
<< "usage: make_torrent
<output torrent-file
> "
220 "<announce url
> <file or directory to create torrent from
>\n
";
227 path full_path = complete(path(argv[
3]));
228 ofstream out(complete(path(argv[
1])), std::ios_base::binary);
230 int piece_size =
256 *
1024;
231 char const* creator_str =
"libtorrent
";
233 add_files(t, full_path.branch_path(), full_path.leaf());
234 t.set_piece_size(piece_size);
237 storage st(t, full_path.branch_path(), fp);
238 t.add_tracker(argv[
2]);
240 // calculate the hash for all pieces
241 int num = t.num_pieces();
242 std::vector
<char
> buf(piece_size);
243 for (int i =
0; i
< num; ++i)
245 st.read(
&buf[
0], i,
0, t.piece_size(i));
246 hasher h(
&buf[
0], t.piece_size(i));
247 t.set_hash(i, h.final());
248 std::cerr
<< (i+
1)
<< "/
" << num
<< "\r
";
251 t.set_creator(creator_str);
253 // create the torrent and print it to out
254 entry e = t.create_torrent();
255 libtorrent::bencode(std::ostream_iterator
<char
>(out), e);
257 catch (std::exception
& e)
259 std::cerr
<< e.what()
<< "\n
";