2013-08-23 65 views
1

我想向對等方發送BitTorrent握手,但它不工作。有人能夠弄清楚我做錯了什麼嗎? len應該回來的東西,但目前爲零。BitTorrent Peer Handshake

require 'bencode' 
require 'digest/sha1' 

file = File.open('./python.torrent').read 
info_hash = Digest::SHA1.hexdigest(a['info'].bencode) 

# Here's what parsed_response['peers'] returns: 
# [{"ip"=>"8.19.35.234", "peer id"=>"-lt0C20-\x90\xE0\xE6\x0E\xD0\x8A\xE5\xA2\xF2b(!",   
# "port"=>9898}] 
peer_id = parsed_response['peers'].first['peer id'] 

send_string = "\023BitTorrent protocol\0\0\0\0\0\0\0\0" << info_hash << peer_id 

# ip and port are my current internet ip and 6881 respectively 
client = TCPSocket.new ip, port 

# What I'm sending over 
client.send("\023BitTorrent protocol\0\0\0\0\0\0\0\0" << info_hash << peer_id, 0) 
len = client.recv(1) 
puts len 

這裏是send_string樣子到底: enter image description here

+0

send_string中的info_hash是一個40字節的hexencoded字符串,它應該是一個20字節的原始二進制字符串。 – Encombe

回答

0

我不能寫紅寶石。 這是Python代碼:

send_string = chr(19)+"BitTorrent Protocol"+8*chr(0)+info_hash+peer_id 
0

雖然有從你身邊握手, 的peerID使用應該是你,而不是對方。

所以你的消息應該是這樣的:

peer_id = "-MY0001-123456654321" 
client.send("\023BitTorrent protocol\0\0\0\0\0\0\0\0" << info_hash << peer_id, 0) 

如果您正在開發自己的位BT客戶端,那麼你可以有同行ID繼位洪流協議中提到的標準你自己的格式。 你可以閱讀更多有關節點ID在這裏: https://wiki.theory.org/BitTorrentSpecification#peer_id

如果你在未來的任何混亂,啓動任何的BitTorrent客戶端,並按照wire-shark包。你會明白,你犯了什麼錯誤。

我在這裏附上一個樣品握手消息: enter image description here

這裏 「-KS0001-123456654321」 是的peerID我的BitTorrent客戶端。