2013-02-03 47 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)。此外,您也可能需要確保在觸發了請求時才觸發請求,以防萬一您在此之前收到「有」消息。