added some precautionary checks in bdecoder
[libtorrent.git] / docs / examples.html
blob682f282d56d289f5578f186c1a3eb12b8d811183
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">
4 <head>
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&#64;rasterbar.com" />
9 <link rel="stylesheet" href="style.css" type="text/css" />
10 </head>
11 <body>
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" />
17 <tbody valign="top">
18 <tr><th class="docinfo-name">Author:</th>
19 <td>Arvid Norberg, <a class="last reference" href="mailto:arvid&#64;rasterbar.com">arvid&#64;rasterbar.com</a></td></tr>
20 </tbody>
21 </table>
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>
24 <ul class="simple">
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>
29 </ul>
30 </li>
31 </ul>
32 </div>
33 <div class="section">
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>
39 <div class="section">
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 &lt;iostream&gt;
45 #include &lt;fstream&gt;
46 #include &lt;iterator&gt;
47 #include &lt;iomanip&gt;
49 #include &quot;libtorrent/entry.hpp&quot;
50 #include &quot;libtorrent/bencode.hpp&quot;
51 #include &quot;libtorrent/torrent_info.hpp&quot;
52 #include &quot;libtorrent/lazy_entry.hpp&quot;
53 #include &lt;boost/filesystem/operations.hpp&gt;
56 int main(int argc, char* argv[])
58 using namespace libtorrent;
59 using namespace boost::filesystem;
61 if (argc != 2)
63 std::cerr &lt;&lt; &quot;usage: dump_torrent torrent-file\n&quot;;
64 return 1;
66 #if BOOST_VERSION &lt; 103400
67 boost::filesystem::path::default_name_check(boost::filesystem::no_check);
68 #endif
70 #ifndef BOOST_NO_EXCEPTIONS
71 try
73 #endif
75 int size = file_size(argv[1]);
76 if (size &gt; 10 * 1000000)
78 std::cerr &lt;&lt; &quot;file too big (&quot; &lt;&lt; size &lt;&lt; &quot;), aborting\n&quot;;
79 return 1;
81 std::vector&lt;char&gt; buf(size);
82 std::ifstream(argv[1], std::ios_base::binary).read(&amp;buf[0], size);
83 lazy_entry e;
84 int ret = lazy_bdecode(&amp;buf[0], &amp;buf[0] + buf.size(), e);
86 if (ret != 0)
88 std::cerr &lt;&lt; &quot;invalid bencoding: &quot; &lt;&lt; ret &lt;&lt; std::endl;
89 return 1;
92 std::cout &lt;&lt; &quot;\n\n----- raw info -----\n\n&quot;;
93 std::cout &lt;&lt; e &lt;&lt; std::endl;
95 torrent_info t(e);
97 // print info about torrent
98 std::cout &lt;&lt; &quot;\n\n----- torrent file info -----\n\n&quot;;
99 std::cout &lt;&lt; &quot;nodes:\n&quot;;
100 typedef std::vector&lt;std::pair&lt;std::string, int&gt; &gt; node_vec;
101 node_vec const&amp; nodes = t.nodes();
102 for (node_vec::const_iterator i = nodes.begin(), end(nodes.end());
103 i != end; ++i)
105 std::cout &lt;&lt; i-&gt;first &lt;&lt; &quot;:&quot; &lt;&lt; i-&gt;second &lt;&lt; &quot;\n&quot;;
107 std::cout &lt;&lt; &quot;trackers:\n&quot;;
108 for (std::vector&lt;announce_entry&gt;::const_iterator i = t.trackers().begin();
109 i != t.trackers().end(); ++i)
111 std::cout &lt;&lt; i-&gt;tier &lt;&lt; &quot;: &quot; &lt;&lt; i-&gt;url &lt;&lt; &quot;\n&quot;;
114 std::cout &lt;&lt; &quot;number of pieces: &quot; &lt;&lt; t.num_pieces() &lt;&lt; &quot;\n&quot;;
115 std::cout &lt;&lt; &quot;piece length: &quot; &lt;&lt; t.piece_length() &lt;&lt; &quot;\n&quot;;
116 std::cout &lt;&lt; &quot;info hash: &quot; &lt;&lt; t.info_hash() &lt;&lt; &quot;\n&quot;;
117 std::cout &lt;&lt; &quot;comment: &quot; &lt;&lt; t.comment() &lt;&lt; &quot;\n&quot;;
118 std::cout &lt;&lt; &quot;created by: &quot; &lt;&lt; t.creator() &lt;&lt; &quot;\n&quot;;
119 std::cout &lt;&lt; &quot;files:\n&quot;;
120 int index = 0;
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-&gt;size - 1, 1).piece;
126 std::cout &lt;&lt; &quot; &quot; &lt;&lt; std::setw(11) &lt;&lt; i-&gt;size
127 &lt;&lt; &quot; &quot; &lt;&lt; i-&gt;path.string() &lt;&lt; &quot;[ &quot; &lt;&lt; first &lt;&lt; &quot;, &quot;
128 &lt;&lt; last &lt;&lt; &quot; ]\n&quot;;
131 #ifndef BOOST_NO_EXCEPTIONS
133 catch (std::exception&amp; e)
135 std::cout &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;;
137 #endif
139 return 0;
141 </pre>
142 </div>
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 &lt; 103400
151 namespace fs = boost::filesystem;
152 fs::path::default_name_check(fs::no_check);
153 #endif
155 if (argc != 2)
157 std::cerr &lt;&lt; &quot;usage: ./simple_client torrent-file\n&quot;
158 &quot;to stop the client, press return.\n&quot;;
159 return 1;
162 #ifndef BOOST_NO_EXCEPTIONS
164 #endif
166 session s;
167 s.listen_on(std::make_pair(6881, 6889));
168 add_torrent_params p;
169 p.save_path = &quot;./&quot;;
170 p.ti = new torrent_info(argv[1]);
171 s.add_torrent(p);
173 // wait for the user to end
174 char a;
175 std::cin.unsetf(std::ios_base::skipws);
176 std::cin &gt;&gt; a;
178 #ifndef BOOST_NO_EXCEPTIONS
179 catch (std::exception&amp; e)
181 std::cout &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;;
183 #endif
184 return 0;
186 </pre>
187 </div>
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 &lt;iostream&gt;
193 #include &lt;fstream&gt;
194 #include &lt;iterator&gt;
195 #include &lt;iomanip&gt;
197 #include &quot;libtorrent/entry.hpp&quot;
198 #include &quot;libtorrent/bencode.hpp&quot;
199 #include &quot;libtorrent/torrent_info.hpp&quot;
200 #include &quot;libtorrent/file.hpp&quot;
201 #include &quot;libtorrent/storage.hpp&quot;
202 #include &quot;libtorrent/hasher.hpp&quot;
203 #include &quot;libtorrent/create_torrent.hpp&quot;
205 #include &lt;boost/filesystem/operations.hpp&gt;
206 #include &lt;boost/filesystem/path.hpp&gt;
207 #include &lt;boost/filesystem/fstream.hpp&gt;
208 #include &lt;boost/bind.hpp&gt;
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&amp; filename)
217 if (filename.leaf()[0] == '.') return false;
218 std::cerr &lt;&lt; filename &lt;&lt; std::endl;
219 return true;
222 void print_progress(int i, int num)
224 std::cerr &lt;&lt; &quot;\r&quot; &lt;&lt; (i+1) &lt;&lt; &quot;/&quot; &lt;&lt; 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 = &quot;libtorrent&quot;;
235 path::default_name_check(no_check);
237 if (argc != 4 &amp;&amp; argc != 5)
239 std::cerr &lt;&lt; &quot;usage: make_torrent &lt;output torrent-file&gt; &quot;
240 &quot;&lt;announce url&gt; &lt;file or directory to create torrent from&gt; &quot;
241 &quot;[url-seed]\n&quot;;
242 return 1;
245 #ifndef BOOST_NO_EXCEPTIONS
248 #endif
249 file_storage fs;
250 file_pool fp;
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(&amp;print_progress, _1, t.num_pieces()));
259 std::cerr &lt;&lt; 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&lt;char&gt;(out), t.generate());
267 #ifndef BOOST_NO_EXCEPTIONS
269 catch (std::exception&amp; e)
271 std::cerr &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;;
273 #endif
275 return 0;
277 </pre>
278 </div>
279 </div>
280 </div>
281 </body>
282 </html>