5 :Author: Arvid Norberg, arvid@rasterbar.com
7 .. contents:: Table of contents
14 Except for the example programs in this manual, there's also a bigger example
15 of a (little bit) more complete client, ``client_test``. There are separate
16 instructions for how to use it here__ if you'd like to try it. Note that building
17 ``client_test`` also requires boost.regex and boost.program_options library.
24 This is an example of a program that will take a torrent-file as a parameter and
25 print information about it to std out::
32 #include "libtorrent/entry.hpp"
33 #include "libtorrent/bencode.hpp"
34 #include "libtorrent/torrent_info.hpp"
35 #include "libtorrent/lazy_entry.hpp"
36 #include <boost/filesystem/operations.hpp>
39 int main(int argc, char* argv[])
41 using namespace libtorrent;
42 using namespace boost::filesystem;
46 std::cerr << "usage: dump_torrent torrent-file\n";
49 #if BOOST_VERSION < 103400
50 boost::filesystem::path::default_name_check(boost::filesystem::no_check);
53 #ifndef BOOST_NO_EXCEPTIONS
58 int size = file_size(argv[1]);
59 if (size > 10 * 1000000)
61 std::cerr << "file too big (" << size << "), aborting\n";
64 std::vector<char> buf(size);
65 std::ifstream(argv[1], std::ios_base::binary).read(&buf[0], size);
67 int ret = lazy_bdecode(&buf[0], &buf[0] + buf.size(), e);
71 std::cerr << "invalid bencoding: " << ret << std::endl;
75 std::cout << "\n\n----- raw info -----\n\n";
76 std::cout << e << std::endl;
80 // print info about torrent
81 std::cout << "\n\n----- torrent file info -----\n\n";
82 std::cout << "nodes:\n";
83 typedef std::vector<std::pair<std::string, int> > node_vec;
84 node_vec const& nodes = t.nodes();
85 for (node_vec::const_iterator i = nodes.begin(), end(nodes.end());
88 std::cout << i->first << ":" << i->second << "\n";
90 std::cout << "trackers:\n";
91 for (std::vector<announce_entry>::const_iterator i = t.trackers().begin();
92 i != t.trackers().end(); ++i)
94 std::cout << i->tier << ": " << i->url << "\n";
97 std::cout << "number of pieces: " << t.num_pieces() << "\n";
98 std::cout << "piece length: " << t.piece_length() << "\n";
99 std::cout << "info hash: " << t.info_hash() << "\n";
100 std::cout << "comment: " << t.comment() << "\n";
101 std::cout << "created by: " << t.creator() << "\n";
102 std::cout << "files:\n";
104 for (torrent_info::file_iterator i = t.begin_files();
105 i != t.end_files(); ++i, ++index)
107 int first = t.map_file(index, 0, 1).piece;
108 int last = t.map_file(index, i->size - 1, 1).piece;
109 std::cout << " " << std::setw(11) << i->size
110 << " " << i->path.string() << "[ " << first << ", "
114 #ifndef BOOST_NO_EXCEPTIONS
116 catch (std::exception& e)
118 std::cout << e.what() << "\n";
128 This is a simple client. It doesn't have much output to keep it simple::
130 int main(int argc, char* argv[])
132 using namespace libtorrent;
133 #if BOOST_VERSION < 103400
134 namespace fs = boost::filesystem;
135 fs::path::default_name_check(fs::no_check);
140 std::cerr << "usage: ./simple_client torrent-file\n"
141 "to stop the client, press return.\n";
145 #ifndef BOOST_NO_EXCEPTIONS
150 s.listen_on(std::make_pair(6881, 6889));
151 add_torrent_params p;
153 p.ti = new torrent_info(argv[1]);
156 // wait for the user to end
158 std::cin.unsetf(std::ios_base::skipws);
161 #ifndef BOOST_NO_EXCEPTIONS
162 catch (std::exception& e)
164 std::cout << e.what() << "\n";
173 Shows how to create a torrent from a directory tree::
180 #include "libtorrent/entry.hpp"
181 #include "libtorrent/bencode.hpp"
182 #include "libtorrent/torrent_info.hpp"
183 #include "libtorrent/file.hpp"
184 #include "libtorrent/storage.hpp"
185 #include "libtorrent/hasher.hpp"
186 #include "libtorrent/create_torrent.hpp"
188 #include <boost/filesystem/operations.hpp>
189 #include <boost/filesystem/path.hpp>
190 #include <boost/filesystem/fstream.hpp>
191 #include <boost/bind.hpp>
193 using namespace boost::filesystem;
194 using namespace libtorrent;
196 // do not include files and folders whose
197 // name starts with a .
198 bool file_filter(boost::filesystem::path const& filename)
200 if (filename.leaf()[0] == '.') return false;
201 std::cerr << filename << std::endl;
205 void print_progress(int i, int num)
207 std::cerr << "\r" << (i+1) << "/" << num;
210 int main(int argc, char* argv[])
212 using namespace libtorrent;
213 using namespace boost::filesystem;
215 int piece_size = 256 * 1024;
216 char const* creator_str = "libtorrent";
218 path::default_name_check(no_check);
220 if (argc != 4 && argc != 5)
222 std::cerr << "usage: make_torrent <output torrent-file> "
223 "<announce url> <file or directory to create torrent from> "
228 #ifndef BOOST_NO_EXCEPTIONS
234 path full_path = complete(path(argv[3]));
236 add_files(fs, full_path, file_filter);
238 create_torrent t(fs, piece_size);
239 t.add_tracker(argv[2]);
240 set_piece_hashes(t, full_path.branch_path()
241 , boost::bind(&print_progress, _1, t.num_pieces()));
242 std::cerr << std::endl;
243 t.set_creator(creator_str);
245 if (argc == 5) t.add_url_seed(argv[4]);
247 // create the torrent and print it to out
248 ofstream out(complete(path(argv[1])), std::ios_base::binary);
249 bencode(std::ostream_iterator<char>(out), t.generate());
250 #ifndef BOOST_NO_EXCEPTIONS
252 catch (std::exception& e)
254 std::cerr << e.what() << "\n";