我正在尝试向对等点发送 BitTorrent 握手,但它不起作用。有人能弄清楚我做错了什么吗? len
应该返回一些东西,但目前为零。
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
最后的样子:
最佳答案
在你身边握手时, 使用的 PeerID 应该是你的,而不是对方的。
所以你的消息应该是这样的:
peer_id = "-MY0001-123456654321"
client.send("\023BitTorrent protocol\0\0\0\0\0\0\0\0" << info_hash << peer_id, 0)
如果您正在开发自己的 bit-torrent 客户端,那么您可以按照 bit-torrent 协议(protocol)提到的标准为 peer id 使用自己的格式。 您可以在此处阅读有关对等 ID 的更多信息: https://wiki.theory.org/BitTorrentSpecification#peer_id
如果您以后有任何困惑,请启动任何 bittorrent 客户端并关注 wire-shark 数据包。你会明白,你在哪里犯了错误。
这里“-KS0001-123456654321”是我的 bittorrent 客户端的 peerid。
关于ruby - BitTorrent 对等握手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18394049/