BitTorrent协议实现:不接收“Piece”消息

时间:2013-02-03 01:44:32

标签: protocols bittorrent

我在Java中实现了大部分BitTorrent协议。问题在于,由于某种原因,没有一个同伴向我发送我要求的任何部分。 (3小时后我只收到2条消息)

我的握手如下:

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

我在另一个帖子中监听消息如下:

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

你能看到这种方法有什么问题吗?我已经尝试过每分钟定期保持活跃的消息,但这没有用。奇怪的是,我收到了大量的bitfield-messages并保持了30多个活动连接。

1 个答案:

答案 0 :(得分:3)

这个答案已经很晚了,但我已经联系到了它,所以这可能仍然有用。

我认为你的ids混淆了。 0是“choke”,1是“unchoke”(不是1和2)。此外,您可能需要确保在未获得解除时触发请求,以防万一在此之前收到“有”消息。