I’ve finished the challenge and it works correctly with the sample torrent and passes all the tests available.
However, it won’t work for any actual torrent file on the internet. I’m not asking about making it production-ready as I understand that it’s out of the scope of a simple learning material and indeed requires lots of time and investment to prepare something production-ready but simple guidelines on how to make it useable on actual files would help it, otherwise it feels like an incomplete project.
as an example, I’m facing 2 issues with real torrent files
it’s not properly parsing torrent info for multifile torrents
I can’t connect to any peer, always getting connection timeout, so probably some additional network magic is needed
It would be helpful to have simple guidelines on how those problems could be tackled for people who are interested to go beyond
Very similar to optimization and download speed tips at the end of the project
After a bit of tinkering I managed to get my implementation to download Ubuntu’s ISO.
I don’t know much more than you do but though I’d just share what I observed.
I have only tried this specific download, maybe if you were to try another file you would observe something entirely different.
The main thing I had to work on is tracking which pieces a peer has using the have and bitfield messages.
If you request a piece your peer does not have there is a good chance it will close your tcp connection.
The challenge implies that the flow of messages is
Send interested
Receive bitfield
Get unchocked
Request pieces
When in practice I have noticed the flow is:
Peer optimistically unchokes me
Send interested
Peer periodically sends have messages to notify me a piece is available for download
You can now request this piece that has been made available
Note there is no bitfield message.
In practice the download is very slow:
I don’t have many peers (sometimes only 1)
I don’t have a list of pieces to request so I’m stuck waiting for pieces to trickle through
Let me know if it helps or if you find anything on your end.