4

I am trying to download a torrent using monotorrent. I have just download a sample program from its github repository and have made required changes.

It is creating all files on respected location with size of 0 bytes and no progress. When running through stack trace, I found that it is throwing an exception saying no connection could be made because the target machine actively refused it. This is an handled exception. I can only see this in stacktrace.

The same torrent can be downloaded by uTorrent program on my windows operating system.

I have set upped the Git repository for my project here

Here is all my code. Is this incomplete code..? What else do I need to add..?

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();
        }
    }
}
gunr2171
  • 16,104
  • 25
  • 61
  • 88
shashwat
  • 7,851
  • 9
  • 57
  • 90

2 Answers2

0

This could be because the tracker in question blocks unknown clients like the one you're developing. Try creating a torrent with Monotorrents own tools and uploading that to a public tracker such as kat.ph

bech
  • 627
  • 5
  • 22
  • I m not creating torrent. All I need to download torrent – shashwat Jul 22 '13 at 07:11
  • 2
    I'm not saying you have to create your own torrent, but try some different torrents so you make sure it's not the tracker blocking third-party clients. – bech Jul 24 '13 at 21:37
  • Some torrent client apps allow you to create a list of clients with permission to connect. There are applications out there that will actively refuse IP addresses on a black list (peer guardian). There are too many variables to debug your code in the wild. – JeremiahDotNet Aug 01 '13 at 19:27
  • I have set upped a Git repository for my project. Please try running this code on your machine if u can or make require changes to the code in repository. https://github.com/goforgold/MonoTorrentSample – shashwat Aug 02 '13 at 11:38
0

If it's any help, I've just managed to successfully integrate MonoTorrent into a project of my own. I basically gave up writing my own code from scratch using the sample code on the website, and instead used the SampleClient in the git repo. You can easily modify that to suit your needs I bet, so try giving that a spin. I'd still say the reason you're getting "Connection was refused"-errors is because you're attempting torrents on trackers that don't allow torrent-applications they don't know to participate. You say you've tried several torrents but these might all be using the same tracker.

HTH.

bech
  • 627
  • 5
  • 22
  • In that case, can u please provide me few links to torrents for those I should give try to...? – shashwat Aug 03 '13 at 14:25
  • I think anything uploaded to kat.ph will work, it did for me anyway. Try using their sample code to create a simple torrent, upload it to Kat.ph and seed it with an existing client while trying to download it with your own. I've succesfully up- and downloaded this torrent (legal torrent, calm down people): http://krasnostav.zombies.nu/archive/DayZero-0.9.9.5.torrent – bech Aug 05 '13 at 10:32