我在一个带有参数的小文本文件上创建了 torrent 文件。
client.exe abc.txt -o abc.torrent -t http://MyIpv4:9090/ -l
创建 torrent 的代码与libtorrent-tutorial-make_torrent中的代码相同,然后在启动客户端时我设置lt::settings_pack::broadcast_lsd
为 true,如下所示
void t_client_class::start_client(string torrent_file_name)
{
lt::settings_pack sp;
sp.set_bool(lt::settings_pack::broadcast_lsd, true);
lt::session s(sp);
lt::add_torrent_params p;
p.save_path = "./";
p.ti = std::make_shared<lt::torrent_info>(torrent_file_name);
s.add_torrent(p);
}
下载种子代码:
bool t_client_class::download_torrent(string magnet_uri)
{
lt::settings_pack p;
p.set_int(lt::settings_pack::alert_mask, lt::alert::status_notification
| lt::alert::error_notification );
p.set_bool(lt::settings_pack::broadcast_lsd, true);
lt::session ses(p);
lt::add_torrent_params atp = lt::parse_magnet_uri(magnet_uri);
atp.save_path = "."; // save in current dir
lt::torrent_handle h = ses.add_torrent(std::move(atp));
for (;;)
{
std::vector<lt::alert*> alerts;
ses.pop_alerts(&alerts);
for (lt::alert const* a : alerts)
{
std::cout << a->message() << std::endl;
// if we receive the finished alert or an error, we're done
if (lt::alert_cast<lt::torrent_finished_alert>(a))
{
goto done;
}
if (lt::alert_cast<lt::torrent_error_alert>(a))
{
goto done;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
done:
std::cout << "done, shutting down" << std::endl;
return true;
}
此代码适用于公共种子。我通过从其中一个 torrent 网站下载有效 torrent 文件中的文件对其进行了测试。
在另一台机器上的同一个 wifi 网络上,我试图通过传递磁铁 URI 来下载这个种子,如下所示
magnet:?xt=urn:btih:<magnet_link_address>=abc.txt&tr=http%3a%2f%2f<IP_where_clinet_started>%3a9090%2f
下载日志
successfully listening on [TCP] 0.0.0.0:6881
successfully listening on [UDP] 0.0.0.0:6881
successfully listening on [TCP] [current_device_mac]:6881
successfully listening on [UDP] [current_device_mac]:6881
successfully listening on [TCP] [current_device_mac_2]:6881
successfully listening on [UDP] [current_device_mac_2]:6881
added torrent: abc.txt
abc.txt: state changed to: dl metadata
abc.txt resumed
abc.txt (http://MyIpv4:9090/)[[current_device_mac]:6881] Unknown error "" (1)
abc.txt (http://MyIpv4:9090/)[[current_device_mac_2]:6881] Unknown error "" (1)
abc.txt (http://MyIpv4:9090/)[0.0.0.0:6881] No connection could be made because the target machine actively refused it "" (1)
abc.txt (http://MyIpv4:9090/)[[current_device_mac]:6881] Unknown error "" (2)
abc.txt (http://MyIpv4:9090/)[[current_device_mac_2]:6881] Unknown error "" (2)
abc.txt (http://MyIpv4:9090/)[0.0.0.0:6881] No connection could be made because the target machine actively refused it "" (2)
我从这个问题中了解到,在 LAN 中我可能必须打开本地对等发现选项并启用 NAT-PMP/UPnP
我尝试过的事情:
- 设置
lt::settings_pack::broadcast_lsd
为true
- 在控制面板 -> 网络和共享 -> 高级设置 -> 公用文件夹共享中启用 windows 网络发现 = 是
- 完全关闭客户端计算机上的防火墙
- 为端口 9090 添加了 TCP 和 UDP 的入站规则
- 如果使用 udp 而不是 http 创建 torrent:在从其他机器运行下载代码时,
An address incompatible with the requested protocol was used.
我无法处理路由器设置,因为我无权访问它。
我能够在同一网络上运行 2 个对等点并发送接收消息而不会出现此问题。
有没有办法克服这个错误?