[bittorrent] Correct way to determine the number of pieces in a torrent?

Robert Seaton seatonr at dupage.edu
Sat Oct 1 13:46:31 EDT 2011

Hey, all,

I'm working on a BitTorrent implementation (
https://github.com/robertseaton/slug) and right now I just have a naive
piece scheduler that downloads each piece in order. Anyways, everything
works fine until I finish downloading the first 1368 pieces out of 1371
pieces. Once I finish downloading piece #1367, suddenly, every seed sends me
a choke message.

My thought is that maybe I've somehow incorrectly calculated the number of
pieces from the torrent metadata and that piece #1367 is actually the last
piece. Currently, to determine the number of pieces, I'm taking the length
of the "pieces:" string from the .torrent, which is 27420, and then dividing
by the length of the SHA1 hash (20), since I assume there is a 1:1 mapping
between pieces and SHA1 hashes.

Is this the correct way to determine the number of pieces in a torrent? If
so, what else would explain the behavior that I'm seeing?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ibiblio.org/pipermail/bittorrent/attachments/20111001/a5789bf8/attachment.html 

More information about the BitTorrent mailing list