Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mono Torrent API

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
GB
Hi there,

I have installed Visual C# 2010 Express and have downloaded the MonoTorrent API and am now trying to download a torrent file. I'm very new to this programming environment - previously just used a text editor for .ASP and .PHP apps as well as vbscript and so on - so please excuse my ignorance!

I have added various "references" which seem to be important and in addition to that I have two programs - one is called GetTorrent.cs and one is called Program.cs

GetTorrent.cs
Code:
using System;
using System.Collections.Generic;
using MonoTorrent.Client;
using MonoTorrent.Client.Encryption;
using System.IO;
using MonoTorrent.Common;
using System.Net;

namespace TorrentDownloader
{

    class MainClass
    {
        ClientEngine engine;
        string savePath;

        // savePath is the directory where downloads will be stored
        public MainClass(string savePath)
        {
            // Create a basic ClientEngine without changing any settings
            this.engine = new ClientEngine(new EngineSettings());
            this.savePath = savePath;
        }

        public void DownloadTorrent(string path)
        {
            // Open the .torrent file
            Torrent torrent = Torrent.Load(path);

            // Create the manager which will download the torrent to savePath
            // using the default settings.
            TorrentManager manager = new TorrentManager(torrent, savePath, new TorrentSettings());

            // Register the manager with the engine
            engine.Register(manager);

            // Begin the download. It is not necessary to call HashCheck on the manager
            // before starting the download. If a hash check has not been performed, the
            // manager will enter the Hashing state and perform a hash check before it
            // begins downloading.

            // If the torrent is fully downloaded already, calling 'Start' will place
            // the manager in the Seeding state.
            manager.Start();
        }
    }
}

Program.cs
Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace TorrentDownloader
{
    class Program
    {
        static void Main(string[] args)
        {
            new MainClass("test");
        }
    }
}

When I click the play button it runs without errors but nothing happens. I know one of the most likely reasons is that I have not specified the name of the .torrent file to download but I can't figure out how to do this. The "test" parameter seems to be where the file gets saved but again I'm not entirely sure on this!

Any help very much appreciated!

Thanks

Ed
 
to begin .cs files are not programs, they are files. these files are compiled into executable code, which is the program.
Typically a file will contain a single object, but that doesn't have to be.

As for your code not failing, but not working. you are only instantiating the object. which sets up the engine and where to save the file. you never call 'DownloadTorrent' which would, most likely, begin the download.
Code:
namespace TorrentDownloader
{
    class Program
    {
        static void Main(string[] args)
        {
            var directory = AppDomain.CurrentDomain.BaseDirectory;
            var uri = "source of the torrent";
            var downloader = new MainClass(directory);
            downloader.DownloadTorrent(uri);
        }
    }
}
something else to consider is whether Start() is asynchronous or not.
If it is asynchronous then you will need to wait for the download to complete before the console app exits. otherwise the application will stop running before the download is complete.
If is synchronous then the console will be locked until the download is complete.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top