我想向對等方發送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
樣子到底:
send_string中的info_hash是一個40字節的hexencoded字符串,它應該是一個20字節的原始二進制字符串。 – Encombe