1

I've implemented most of the BitTorrent protocol in Java. The problem is that for some reason none of the peers are sending me any of the pieces I request. (After 3 hours I received only 2 piece-messages)

My handshaking is as follows:

send 19
send "BitTorrent protocol"
send 8 zero'd reserved bytes
send info_hash (20 bytes)
send peer_id (20 bytes)

read 19
read "BitTorrent protocol"
read 8 reserved bytes
read info_hash. Compare with own info_hash
read peer_id

send unchoke

start listening for messages

I listen for messages in another thread as follows:

while(true)
    read length (4 bytes)
    read id (1 byte)
    if length == 0: continue //keep-alive message
    if id == 1: Stop all requests to this peer
    if id == 2: Continue all requests to this peer
    if id == 4: Read index from have-message and request piece if we don't have it
    if id == 5: If we have not already received bitfield. Read and store it. Request any pieces that we don't have yet
    if id == 7: Read index, begin and length. Read and store piece. Send have-message if a piece was fully downloaded

Can you see any problem with this approach? I've tried doing periodic keep-alive messages every minute, but that doesn't help. What's weird is that I'm receiving tons of bitfield-messages and maintaining 30+ active connections.

4

1 回答 1

3

这个答案已经很晚了,但我已经链接到它,所以也许这仍然有用。

我想你把你的身份证弄混了。0 是“阻塞”,1 是“不阻塞”(不是 1 和 2)。此外,您可能还需要确保在您不被阻塞时触发请求,以防万一您在此之前收到“拥有”消息。

于 2013-05-10T21:35:00.763 回答