我正在尝试向对等设备发送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最后的样子:

发布于 2016-06-12 15:10:35
当你与对方握手时,使用的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协议中提到的标准来使用你自己的对等体id格式。您可以在此处阅读有关对等id的更多信息:https://wiki.theory.org/BitTorrentSpecification#peer_id
如果你以后有任何困惑,可以启动任何bittorrent客户端并跟踪wire-shark数据包。你会明白,你在哪里犯了错。
我在这里附加了一个握手消息示例:

这里"-KS0001-123456654321“是我的bittorrent客户端的peerid。
发布于 2014-02-01 03:08:20
我不会写红宝石。这是python代码:
send_string = chr(19)+"BitTorrent Protocol"+8*chr(0)+info_hash+peer_idhttps://stackoverflow.com/questions/18394049
复制相似问题