2013-02-03 45 views
1

我已经实现了Java中的大部分BitTorrent协议。问题是,由于某种原因,没有一个同伴向我发送任何我请求的作品。 (3小时后,我只收到2件的消息)BitTorrent协议实现:未收到“Piece”消息

我握手如下:

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 

你能看到的任何这种方法的问题?我试过每分钟都要定期保存一次,但这并没有帮助。奇怪的是,我收到了大量的位域消息并保持了30多个活动连接。

回答

2

这个答案很晚,但是我把它链接到了它,所以这可能仍然有用。

我想你已经混淆了你的ID。 0是“扼流”,1是“解锁”(不是1和2)。此外,您也可能需要确保在触发了请求时才触发请求,以防万一您在此之前收到“有”消息。