首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用monotorrent下载torrent (“无法连接,因为目标计算机主动拒绝”)

无法使用monotorrent下载torrent (“无法连接,因为目标计算机主动拒绝”)
EN

Stack Overflow用户
提问于 2013-07-13 16:51:21
回答 2查看 3.1K关注 0票数 4

我正在尝试使用monotorrent下载torrent。我刚刚从它的github repository下载了一个示例程序,并进行了必要的更改。

它正在尊重的位置上创建大小为0字节的所有文件,并且没有任何进展。在运行堆栈跟踪时,我发现它抛出了一个异常,声明为no connection could be made because the target machine actively refused it。这是一个已处理的异常。我只能在堆栈跟踪中看到这一点。

在我的windows操作系统上,可以通过uTorrent程序下载same torrent

I have set upped the Git repository for my project here

这是我所有的代码。这是不完整的代码吗?我还需要添加什么?

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using MonoTorrent.Client;
using MonoTorrent.Client.Encryption;
using System.IO;
using MonoTorrent.Common;
using System.Net;
using System.Web;
using MonoTorrent.Tracker;
using MonoTorrent.Tracker.Listeners;

namespace Samples
{
    public class ClientSample
    {
        BanList banlist;
        ClientEngine engine;
        List<TorrentManager> managers = new List<TorrentManager>();

        public ClientSample()
        {
            //StartTracker();
            SetupEngine();
            //SetupBanlist();
            LoadTorrent();
            StartTorrents();
        }

        void SetupEngine()
        {
            EngineSettings settings = new EngineSettings();
            settings.AllowedEncryption = ChooseEncryption();

            // If both encrypted and unencrypted connections are supported, an encrypted connection will be attempted
            // first if this is true. Otherwise an unencrypted connection will be attempted first.
            settings.PreferEncryption = true;

            // Torrents will be downloaded here by default when they are registered with the engine
            //settings.SavePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Torrents");
            settings.SavePath = HttpContext.Current.Request.MapPath("~/Torrents/");

            // The maximum upload speed is 200 kilobytes per second, or 204,800 bytes per second
            settings.GlobalMaxUploadSpeed = 200 * 1024;

            engine = new ClientEngine(settings);

            // Tell the engine to listen at port 6969 for incoming connections
            engine.ChangeListenEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6969));
        }

        EncryptionTypes ChooseEncryption()
        {
            EncryptionTypes encryption;
            // This completely disables connections - encrypted connections are not allowed
            // and unencrypted connections are not allowed
            encryption = EncryptionTypes.None;

            // Only unencrypted connections are allowed
            encryption = EncryptionTypes.PlainText;

            // Allow only encrypted connections
            encryption = EncryptionTypes.RC4Full | EncryptionTypes.RC4Header;

            // Allow unencrypted and encrypted connections
            encryption = EncryptionTypes.All;
            encryption = EncryptionTypes.PlainText | EncryptionTypes.RC4Full | EncryptionTypes.RC4Header;

            return encryption;
        }

        void SetupBanlist()
        {
            banlist = new BanList();

            if (!File.Exists("banlist"))
                return;

            // The banlist parser can parse a standard block list from peerguardian or similar services
            BanListParser parser = new BanListParser();
            IEnumerable<AddressRange> ranges = parser.Parse(File.OpenRead("banlist"));
            banlist.AddRange(ranges);

            // Add a few IPAddress by hand
            banlist.Add(IPAddress.Parse("12.21.12.21"));
            banlist.Add(IPAddress.Parse("11.22.33.44"));
            banlist.Add(IPAddress.Parse("44.55.66.77"));

            engine.ConnectionManager.BanPeer += delegate(object o, AttemptConnectionEventArgs e)
            {
                IPAddress address;

                // The engine can raise this event simultaenously on multiple threads
                if (IPAddress.TryParse(e.Peer.ConnectionUri.Host, out address))
                {
                    lock (banlist)
                    {
                        // If the value of e.BanPeer is true when the event completes,
                        // the connection will be closed. Otherwise it will be allowed
                        e.BanPeer = banlist.IsBanned(address);
                    }
                }
            };
        }

        void LoadTorrent()
        {
            // Load a .torrent file into memory
            //Torrent torrent = Torrent.Load("myfile.torrent");
            Torrent torrent = Torrent.Load(HttpContext.Current.Request.MapPath("~/myfile.torrent"));

            // Set all the files to not download
            foreach (TorrentFile file in torrent.Files)
                file.Priority = Priority.Normal;

            // Set the first file as high priority and the second one as normal
            //torrent.Files[0].Priority = Priority.Highest;
            //torrent.Files[1].Priority = Priority.Normal;

            //TorrentManager manager = new TorrentManager(torrent, "DownloadFolder", new TorrentSettings());
            TorrentManager manager = new TorrentManager(torrent, HttpContext.Current.Request.MapPath("~/Torrents/"), new TorrentSettings());

            managers.Add(manager);
            engine.Register(manager);

            // Disable rarest first and randomised picking - only allow priority based picking (i.e. selective downloading)
            PiecePicker picker = new StandardPicker();
            picker = new PriorityPicker(picker);
            manager.ChangePicker(picker);
        }

        void StartTorrents()
        {
            engine.StartAll();
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2013-07-22 04:19:30

这可能是因为有问题的跟踪器阻止了像你正在开发的这样的未知客户端。尝试使用Monotorrents自己的工具创建torrent,并将其上传到公共跟踪器,如kat.ph

票数 0
EN

Stack Overflow用户

发布于 2013-08-03 05:20:49

如果有任何帮助的话,我刚刚成功地将MonoTorrent集成到了我自己的项目中。我基本上放弃了使用网站上的示例代码从头开始编写自己的代码,而是使用git代码库中的SampleClient。我敢打赌,你可以很容易地修改它来满足你的需求,所以试着让它旋转一下。我仍然会说,你得到“连接被拒绝”-errors的原因是因为你试图在跟踪器上不允许torrent-他们不知道的应用程序参与的torrent。您说您已经尝试了几个torrents,但这些可能都使用相同的跟踪器。

HTH。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17628553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档