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.4: 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
"
52 #include
"libtorrent/lazy_entry.hpp
"
53 #include
<boost/filesystem/operations.hpp
>
56 int main(int argc, char* argv[])
58 using namespace libtorrent;
59 using namespace boost::filesystem;
63 std::cerr
<< "usage: dump_torrent torrent-file\n
";
66 #if BOOST_VERSION
< 103400
67 boost::filesystem::path::default_name_check(boost::filesystem::no_check);
70 #ifndef BOOST_NO_EXCEPTIONS
75 int size = file_size(argv[
1]);
76 if (size
> 10 *
1000000)
78 std::cerr
<< "file too big (
" << size
<< "), aborting\n
";
81 std::vector
<char
> buf(size);
82 std::ifstream(argv[
1], std::ios_base::binary).read(
&buf[
0], size);
84 int ret = lazy_bdecode(
&buf[
0],
&buf[
0] + buf.size(), e);
88 std::cerr
<< "invalid bencoding:
" << ret
<< std::endl;
92 std::cout
<< "\n\n----- raw info -----\n\n
";
93 std::cout
<< e
<< std::endl;
97 // print info about torrent
98 std::cout
<< "\n\n----- torrent file info -----\n\n
";
99 std::cout
<< "nodes:\n
";
100 typedef std::vector
<std::pair
<std::string, int
> > node_vec;
101 node_vec const
& nodes = t.nodes();
102 for (node_vec::const_iterator i = nodes.begin(), end(nodes.end());
105 std::cout
<< i-
>first
<< ":
" << i-
>second
<< "\n
";
107 std::cout
<< "trackers:\n
";
108 for (std::vector
<announce_entry
>::const_iterator i = t.trackers().begin();
109 i != t.trackers().end(); ++i)
111 std::cout
<< i-
>tier
<< ":
" << i-
>url
<< "\n
";
114 std::cout
<< "number of pieces:
" << t.num_pieces()
<< "\n
";
115 std::cout
<< "piece length:
" << t.piece_length()
<< "\n
";
116 std::cout
<< "info hash:
" << t.info_hash()
<< "\n
";
117 std::cout
<< "comment:
" << t.comment()
<< "\n
";
118 std::cout
<< "created by:
" << t.creator()
<< "\n
";
119 std::cout
<< "files:\n
";
121 for (torrent_info::file_iterator i = t.begin_files();
122 i != t.end_files(); ++i, ++index)
124 int first = t.map_file(index,
0,
1).piece;
125 int last = t.map_file(index, i-
>size -
1,
1).piece;
126 std::cout
<< " " << std::setw(
11)
<< i-
>size
127 << " " << i-
>path.string()
<< "[
" << first
<< ",
"
128 << last
<< " ]\n
";
131 #ifndef BOOST_NO_EXCEPTIONS
133 catch (std::exception
& e)
135 std::cout
<< e.what()
<< "\n
";
143 <div class=
"section">
144 <h2><a id=
"simple-client" name=
"simple-client">simple client
</a></h2>
145 <p>This is a simple client. It doesn't have much output to keep it simple:
</p>
146 <pre class=
"literal-block">
147 int main(int argc, char* argv[])
149 using namespace libtorrent;
150 #if BOOST_VERSION
< 103400
151 namespace fs = boost::filesystem;
152 fs::path::default_name_check(fs::no_check);
157 std::cerr
<< "usage: ./simple_client torrent-file\n
"
158 "to stop the client, press return.\n
";
162 #ifndef BOOST_NO_EXCEPTIONS
167 s.listen_on(std::make_pair(
6881,
6889));
168 add_torrent_params p;
169 p.save_path =
"./
";
170 p.ti = new torrent_info(argv[
1]);
173 // wait for the user to end
175 std::cin.unsetf(std::ios_base::skipws);
178 #ifndef BOOST_NO_EXCEPTIONS
179 catch (std::exception
& e)
181 std::cout
<< e.what()
<< "\n
";
188 <div class=
"section">
189 <h2><a id=
"make-torrent" name=
"make-torrent">make_torrent
</a></h2>
190 <p>Shows how to create a torrent from a directory tree:
</p>
191 <pre class=
"literal-block">
192 #include
<iostream
>
193 #include
<fstream
>
194 #include
<iterator
>
195 #include
<iomanip
>
197 #include
"libtorrent/entry.hpp
"
198 #include
"libtorrent/bencode.hpp
"
199 #include
"libtorrent/torrent_info.hpp
"
200 #include
"libtorrent/file.hpp
"
201 #include
"libtorrent/storage.hpp
"
202 #include
"libtorrent/hasher.hpp
"
203 #include
"libtorrent/create_torrent.hpp
"
205 #include
<boost/filesystem/operations.hpp
>
206 #include
<boost/filesystem/path.hpp
>
207 #include
<boost/filesystem/fstream.hpp
>
208 #include
<boost/bind.hpp
>
210 using namespace boost::filesystem;
211 using namespace libtorrent;
213 // do not include files and folders whose
214 // name starts with a .
215 bool file_filter(boost::filesystem::path const
& filename)
217 if (filename.leaf()[
0] == '.') return false;
218 std::cerr
<< filename
<< std::endl;
222 void print_progress(int i, int num)
224 std::cerr
<< "\r
" << (i+
1)
<< "/
" << num;
227 int main(int argc, char* argv[])
229 using namespace libtorrent;
230 using namespace boost::filesystem;
232 int piece_size =
256 *
1024;
233 char const* creator_str =
"libtorrent
";
235 path::default_name_check(no_check);
237 if (argc !=
4 && argc !=
5)
239 std::cerr
<< "usage: make_torrent
<output torrent-file
> "
240 "<announce url
> <file or directory to create torrent from
> "
241 "[url-seed]\n
";
245 #ifndef BOOST_NO_EXCEPTIONS
251 path full_path = complete(path(argv[
3]));
253 add_files(fs, full_path, file_filter);
255 create_torrent t(fs, piece_size);
256 t.add_tracker(argv[
2]);
257 set_piece_hashes(t, full_path.branch_path()
258 , boost::bind(
&print_progress, _1, t.num_pieces()));
259 std::cerr
<< std::endl;
260 t.set_creator(creator_str);
262 if (argc ==
5) t.add_url_seed(argv[
4]);
264 // create the torrent and print it to out
265 ofstream out(complete(path(argv[
1])), std::ios_base::binary);
266 bencode(std::ostream_iterator
<char
>(out), t.generate());
267 #ifndef BOOST_NO_EXCEPTIONS
269 catch (std::exception
& e)
271 std::cerr
<< e.what()
<< "\n
";