Coffeehouse Thread

53 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

BitTorrent client in C#

Back to Forum: Coffeehouse
  • User profile image
    Rossj

     I know I've looked for one in the past, but if you have need of one you can get info about it at Miguel De Icaza's blog (I still read it even after our little tiff - imaginary). Even better it is under the X11 license, so you can do pretty much anything with it (apart from claim it as your own).

    Apparently it came about from the Summer of Code, so we can thank the Mono guys and Google. I am now awaiting a flood of .Net BT clients Smiley

  • User profile image
    ElucidWeb

    Rossj wrote:
     I know I've looked for one in the past, but if you have need of one you can get info about it at Miguel De Icaza's blog (I still read it even after our little tiff - imaginary). Even better it is under the X11 license, so you can do pretty much anything with it (apart from claim it as your own).

    Apparently it came about from the Summer of Code, so we can thank the Mono guys and Google. I am now awaiting a flood of .Net BT clients


    I actually wrote a good chunk of a BitTorrent application to Digest the Bencoded format that the .Torrent files are encoded in and translate those into an object oriented meta class... But if your looking for a fully finished on than BTSharp is a completely 100% fully functionaly bittorrent client written in .NET, last I checked you could download the source code for it also!!

    Have fun, you can google for it for the link im being lazy!

  • User profile image
    Ion Todirel

    ElucidWeb wrote:
    
    Rossj wrote:  I know I've looked for one in the past, but if you have need of one you can get info about it at Miguel De Icaza's blog (I still read it even after our little tiff - imaginary). Even better it is under the X11 license, so you can do pretty much anything with it (apart from claim it as your own).

    Apparently it came about from the Summer of Code, so we can thank the Mono guys and Google. I am now awaiting a flood of .Net BT clients


    I actually wrote a good chunk of a BitTorrent application to Digest the Bencoded format that the .Torrent files are encoded in and translate those into an object oriented meta class...
    you said it Big Smile can you share ?

  • User profile image
    Angus

    There is this C# Bit Torrent client:

    [Link]

    But it uses an unmanaged C++ Library for all the downloading. It would be nice to see an open source, fully managed code Bit Torrent Client written in C#.

    Angus Higgins

  • User profile image
    TimP

    Angus wrote:
    would be nice to see an open source, fully managed code Bit Torrent Client written in C#.

    Angus Higgins


    That's what the people Rossj was referring to did.

  • User profile image
    ElucidWeb

    Sure you can download my solution at www.elucidweb.com/files/bencoding.zip

    You may want to keep in mine however that this was my initial attempt, I later found out that all Bencoding files should be read in UTF8 formated while I was reading them in ASCII, that poses a pretty big issue with my current code since in ASCII all characters are represented by 1 byte but in UTF8 one character can be represented by 1-4 bytes, my solution was to rewrite the entire thing using a MemoryStream that way I could deal with it at a lower level...

    Its a hard thing to test if its really being effected by ASCII or not, the pieces have to be Hashed and the Hash value is what a Bit Torrent client uses as its tracking mechanism for each 'Piece" or chunk to download.  I havent found a way to verify that the Hash is correct, its a difficult prospect to debug.

    In any case have fun...

  • User profile image
    alanmc

    Rossj wrote:
    Even better it is under the X11 license, so you can do pretty much anything with it (apart from claim it as your own).

    Yup, i'd be very unhappy if i found people claiming my library as their own work. In fact, i might become so happy as to sign their email address up for dubious content! Tongue Out

    Yes, i'm the guy who wrote the C# library. I was just doing some googling to see if anyone was talking about it, and look what i found Wink What i can say is that so far there is 1 basic GUI created and there are 2 people who have started checking out the library for embedding into applications (auto-update just became so much easier). So, less than 3 weeks after it was officially released, i have three uses (hopefully Tongue Out).

    If there is anyone out there who wants to make a GUI, or help improve the existing GUI (the better option) give me an email. You can find my info plastered all over the code files Tongue Out

    Just in case any of ye are worried about performance, think of my library as being more like uTorrent than like Azureus. It's not quite as efficient (a limitation of being in a managed language) but it's nowhere near as much of a memory hog as azureus.

    As for BTSharp, well, i have one HUGE advantage over it (without even looking at it's code). My library is Open Source. I'm not going to charge you for using it (but i would like donations Wink ). Other than that, there is an OS Client Library, and OS Tracker library completed as part of the SoC. So what is offered by OS is just as good as BTSharp.


    For those of you looking for a .NET BEncoding library, it's an easy task to extract the BEncoding routine from my library and compile it into a distributable DLL (but if someone else does that you should make it clear where the code came from Tongue Out ).

    Code is at http://svn.myrealbox.com/viewcvs/trunk/bitsharp/ if anyone wants to use it.

  • User profile image
    alanmc

    ElucidWeb wrote:
    I later found out that all Bencoding files should be read in UTF8 formated

    You're right and you're wrong. Any string that has been bencoded should have initially been UTF8, but you can't treat a BEncodedString as valid UTF8 as the pieces are encoded as a BEncodedString but in fact they are a byte[]. If you *ever* convert the pieces value into a .NET string, you've destroyed it.

    What you have to do is internally treat all BEncodedStrings as byte[], but give your user an option to convert that byte[] to Text (using System.Text.Encoding.UTF8.Decode(byte[])Wink.

    ElucidWeb wrote:
    
    my solution was to rewrite the entire thing using a MemoryStream that way I could deal with it at a lower level...

    That approach is what i used, it works fine once the above is taken into account.


    ElucidWeb wrote:
    
    Its a hard thing to test if its really being effected by ASCII or not, the pieces have to be Hashed and the Hash value is what a Bit Torrent client uses as its tracking mechanism for each 'Piece" or chunk to download.  I havent found a way to verify that the Hash is correct, its a difficult prospect to debug.

    The easiest way to check if the infohash calculated from the info dictionary is correct is to:
    1) Hash the info dictionary in your library.
    2) Take the resulting byte array and convert it to HEX using BitConverter.ToString(byte[]).
    3) Open the same .torrent in Azureus and see if Azureus is displaying the same hex'ed string.

    But what you're doing is wrong Tongue Out The "pieces" BEncodedString is the already-hashed values. You just need to read out the X bytes that the string is, and then split that byte[] up into chunks of length 20. You don't hash those chunks.


    One thing you'll need for your classes is an Encode method aswell. YOu need to be able to create .torrent files too Wink Also, BEncoding will be used in some extensions to the BT protocol, so you need to encode for that.

    Lastly, i just noticed that your BEncodedDictionary is just a regular dictionary. That won't work. You need to use a sorted dictionary. The specs say that the keys in a BEncodedDictionary must be in sorted order. So a SortedDictionary guarantees that. (You still need to throw an exception if you find that as you decode a dictionary, the keys aren't in the right order).

  • User profile image
    Rossj

    alanmc wrote:
    
    Rossj wrote:Even better it is under the X11 license, so you can do pretty much anything with it (apart from claim it as your own).

    Yup, i'd be very unhappy if i found people claiming my library as their own work. In fact, i might become so happy as to sign their email address up for dubious content!


    That's why I made a point of mentioning it, I am not sure there is anything worse* than people seeing something under any OSS licence and assuming they can claim it for their own.

    I was thinking specifically of a BT client on an XBox 360 - but given the absence of a Socket lib (at least in XNA) it is probably not a go-er.

    alanmc wrote:
    
    Yes, i'm the guy who wrote the C# library. I was just doing some googling to see if anyone was talking about it, and look what i found


    Oooh - egosurfing... Wink

    alanmc wrote:
    
    Code is at http://svn.myrealbox.com/viewcvs/trunk/bitsharp/ if anyone wants to use it.


    As well as the svn repo, is there a website for it?



    * Other than Apple releasing more products than I can afford to buy - that's worse ... okay - maybe not.

  • User profile image
    alanmc

    At the moment, there's no website. I will try and get someone (hopefully not me, but i will if i have to) to throw together something for me so i can upload it. I have some webspace, but i don't have the time or interest in designing a nice looking webpage. If i do it, it'll look horrible Tongue Out


    There's nothing wrong with ego surfing... if i google for my name i'm 7/10 of the first page of results Tongue Out

  • User profile image
    Rossj

    alanmc wrote:
    At the moment, there's no website. I will try and get someone (hopefully not me, but i will if i have to) to throw together something for me so i can upload it. I have some webspace, but i don't have the time or interest in designing a nice looking webpage. If i do it, it'll look horrible


    Blog?

  • User profile image
    Rossj

    Minh wrote:
    The .Net Rocks show used to have a guy on that created just this... a C# bittorrent library & client... Will hunt when I have some more time.



    I tried looking for that before, but kept finding only half finished web-pages and no actual code - and I don't know what licence it was supposed to be available under Sad

  • User profile image
    Minh

    The .Net Rocks show used to have a guy on that created just this... a C# bittorrent library & client... Will hunt when I have some more time.

    Edit:

    The .Net Rocks Show # 160

    BTSharp (free for open-sourced & free projects, comercial coming soon)

  • User profile image
    Minh

    Rossj wrote:

    I tried looking for that before, but kept finding only half finished web-pages and no actual code - and I don't know what licence it was supposed to be available under
    His page was up & down when I was looking a few months back, but it seems to be up now.

    He's saying that the library is free for open-sourced & free projects .... and a commercial license is coming...

  • User profile image
    alanmc

    *gratuitious self promotion*

    My client library and gregors (the guy who wrote a tracker for summer of code) tracker are both cross platform. They can be verified to work on linux, windows and macos. They work out of box on any platform that Mono or the MS.NET framework supports (which is quite a lot!). On the otherhand, BTSharp (currently) is windows only. I just tested it.

    So while it's written in a crossplatform language, it wasn't written in a cross-platform way. Of course, it might be an easy fix, then again it might not. The author would probably update his library to be more cross-platform if enough people bugged him.

  • User profile image
    ElucidWeb

    alanmc wrote:
    
    ElucidWeb wrote: I later found out that all Bencoding files should be read in UTF8 formated

    You're right and you're wrong. Any string that has been bencoded should have initially been UTF8, but you can't treat a BEncodedString as valid UTF8 as the pieces are encoded as a BEncodedString but in fact they are a byte[]. If you *ever* convert the pieces value into a .NET string, you've destroyed it.

    What you have to do is internally treat all BEncodedStrings as byte[], but give your user an option to convert that byte[] to Text (using System.Text.Encoding.UTF8.Decode(byte[]).

    ElucidWeb wrote: 
    my solution was to rewrite the entire thing using a MemoryStream that way I could deal with it at a lower level...

    That approach is what i used, it works fine once the above is taken into account.


    ElucidWeb wrote: 
    Its a hard thing to test if its really being effected by ASCII or not, the pieces have to be Hashed and the Hash value is what a Bit Torrent client uses as its tracking mechanism for each 'Piece" or chunk to download.  I havent found a way to verify that the Hash is correct, its a difficult prospect to debug.

    The easiest way to check if the infohash calculated from the info dictionary is correct is to:
    1) Hash the info dictionary in your library.
    2) Take the resulting byte array and convert it to HEX using BitConverter.ToString(byte[]).
    3) Open the same .torrent in Azureus and see if Azureus is displaying the same hex'ed string.

    But what you're doing is wrong The "pieces" BEncodedString is the already-hashed values. You just need to read out the X bytes that the string is, and then split that byte[] up into chunks of length 20. You don't hash those chunks.


    One thing you'll need for your classes is an Encode method aswell. YOu need to be able to create .torrent files too Also, BEncoding will be used in some extensions to the BT protocol, so you need to encode for that.

    Lastly, i just noticed that your BEncodedDictionary is just a regular dictionary. That won't work. You need to use a sorted dictionary. The specs say that the keys in a BEncodedDictionary must be in sorted order. So a SortedDictionary guarantees that. (You still need to throw an exception if you find that as you decode a dictionary, the keys aren't in the right order).


    Thanks for the criticism alan, some of those were issues I am already aware of but some of them were new to me.  My initial attempt was all based on documentation I could find online and alot of it contradicted itself well some of it at least.  You are right, I didnt write a mechanism to support creation of torrents that was a second iteration requirement I had set for myself.  My first iteration requirement was that it had to read the torrent and build an in memory representation of this in a very object oriented fasion, basically a class representing the torrent. 

    I didnt realize so many people have been working on this, it all started for me with trying to choose a very difficult project that no one else had done in .NET, suprising to me that so many people have already done this in .NET.

    Needlesss to say its taught me quit a few things and I will keep working towards my goal even if I am replicating efforts of others.  Perhaps we can collaborate our efforts sometime and release a .NET BitTorrent application worthy enough to compete against Azureus.

  • User profile image
    Chadk

    Thanks for the links. I have a project that this is very usefull!

    Theres 2 different projects. Any have experience with one or more of them? Which is best?

  • User profile image
    alanmc

    To be perfectly honest with you, BTSharp is (more than likely, but i can't guarantee Tongue Out) more stable than my library. I'm at release version 0.1 whereas BTSharp is on a stable 1.0. On the other hand, my library is both open source and still under development so you can easily request more features that you might need yourself that you think should be provided by the library. If there's a bug, you can fix it yourself and submit a patch if ya want Tongue Out

    My library is just as easy to use as BTSharp. They both have very similar syntax for accessing the downloading engine. My library does have a lot more information displayable though, and (some things) about my API are a little clearer.


    Ok... so features that my library HAS that are not found in BTSharp (after a quick lookthrough):
    1) selective downloading of files
    2) file prioritisation
    3) Both download and upload speed limits on a per torrent and overall basis (overall speed limits aren't obeyed as of yet, give it a few days)
    4) You can actually see what files are available to be downloaded
    5) Supports Multiple Trackers
    6) Fast Peers extension
    7) Support multiple trackers

    Hell, BTSharp crashes on legitimate tracker responses. If a torrent is not found on the tracker that BTSharp tries to download, it crashes. BTSharp just blindly assumes that certain values are in a tracker response, but they mightn't necessarily be there Perplexed


    NOTE: MonoTorrent isn't 100% stable either, i'm not claiming it is. But at least it will open every valid .torrent file and it throws proper exceptions on invalid ones.


    So even after saying that, i'd probably still recommend BTSharp unless you want to be on the bleeding edge of torrent development Tongue Out

    EDIT: WikiPage for MonoTorrent: http://mono-project.com/Bitsharp


    EDIT: @Lucidweb: The documentation is HORRIBLE for bittorrent. The best site is wiki.theory.org/BitTorrentSpecification. So check that out. The only reason i see all your mistakes is because i made each and every one of those initially. So you're not doing badly Tongue Out It took me ages after the first write of my BEncoding class to actually have a fully working one, and even now i'd only consider it 99% ok. There's probably something somewhere that'd break it Tongue Out

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.