打开Halite solution,在solution下新建一个console工程比如PatchP2P,然后在该工程下新建一个cpp文件比如PathcP2P.cpp,然后把这个文件拷贝进去(这个cpp来自libtorrent的doc中的example):
- #include <iostream>
- #include <fstream>
- #include <iterator>
- #include <iomanip>
- #include "libtorrent/entry.hpp"
- #include "libtorrent/bencode.hpp"
- #include "libtorrent/torrent_info.hpp"
- int main(int argc, char* argv[])
- {
- using namespace libtorrent;
- if (argc != 2)
- {
- std::cerr << "usage: dump_torrent torrent-file/n";
- return 1;
- }
- try
- {
- std::ifstream in(argv[1], std::ios_base::binary);
- in.unsetf(std::ios_base::skipws);
- entry e = bdecode(std::istream_iterator<char>(in)
- , std::istream_iterator<char>());
- std::cout << "/n/n----- raw info -----/n/n";
- e.print(std::cout);
- torrent_info t(e);
- // print info about torrent
- std::cout << "/n/n----- torrent file info -----/n/n";
- std::cout << "nodes:/n";
- typedef std::vector<std::pair<std::string, int> > node_vec;
- node_vec const& nodes = t.nodes();
- for (node_vec::const_iterator i = nodes.begin(), end(nodes.end());
- i != end; ++i)
- {
- std::cout << i->first << ":" << i->second << "/n";
- }
- std::cout << "trackers:/n";
- for (std::vector<announce_entry>::const_iterator i
- = t.trackers().begin(); i != t.trackers().end(); ++i)
- {
- std::cout << i->tier << ": " << i->url << "/n";
- }
- std::cout << "number of pieces: " << t.num_pieces() << "/n";
- std::cout << "piece length: " << t.piece_length() << "/n";
- std::cout << "info hash: " << t.info_hash() << "/n";
- std::cout << "comment: " << t.comment() << "/n";
- std::cout << "created by: " << t.creator() << "/n";
- std::cout << "files:/n";
- for (torrent_info::file_iterator i = t.begin_files();
- i != t.end_files(); ++i)
- {
- std::cout << " " << std::setw(11) << i->size
- << " " << i->path.string() << "/n";
- }
- }
- catch (std::exception& e)
- {
- std::cout << e.what() << "/n";
- }
- getchar();
- return 0;
- }
设置工程属性,把c/c++ -> 代码生成 -> 运行时库设为“多线程调试(/MTd)”,把c/c++ -> 预处理器 -> 预处理器定义设为
“WIN32_LEAN_AND_MEAN;_WIN32_WINNT=0x0500;__USE_W32_SOCKETS;WIN32;_WIN32;_WINDOWS;_DEBUG;TORRENT_USE_BOOST_DATE_TIME;TORRENT_USE_OPENSSL;_CRT_SECURE_NO_DEPRECATE;_SCL_SECURE_NO_
DEPRECATE;_CRT_SECURE_NO_WARNINGS;_SECURE_SCL=0”,然后将libtorrent,asio,boost1.34,openssl的include目录加入c/c++ -> 常规 -> 附加包含目录,可以这样添加:$(SolutionDir)
lib/libtorrent/include";"$(SolutionDir)lib/libtorrent/zlib";"$(SolutionDir)lib/Asio/include/";"$(SolutionDir)lib/Openssl/inc";E:/9c/boost134/boost_1_34_1。在链接器 -> 常规 -> 附
加库目录中加入boost1.34和libtorrent的lib文件所在的目录,比如E:/9c/boost134/boost_1_34_1/stage/lib;E:/9c/P2P/Halite/Halite.0_3_1.src/debug,在链接器 -> 输入 -> 附加依赖项中加入
libtorrent和openssl的lib,比如libtorrent.lib "$(SolutionDir)lib/openssl/bin/debug/link-static/runtime-link-static/threading-multi/libeay32.lib" "$(SolutionDir)
lib/openssl/bin/debug/link-static/runtime-link-static/threading-multi/ssleay32.lib"。然后编译成PatchP2P.exe文件,在命令行下运行,参数为某个torrent文件,程序运行的结果是打印出该
torrent文件的信息。