首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BitTorrent对等握手

BitTorrent对等握手
EN

Stack Overflow用户
提问于 2013-08-23 11:08:20
回答 2查看 1.7K关注 0票数 1

我正在尝试向对等设备发送BitTorrent握手,但它不起作用。有人能指出我做错了什么吗?len应该返回一些东西,但现在是零。

代码语言:javascript
复制
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最后的样子:

EN

回答 2

Stack Overflow用户

发布于 2016-06-12 15:10:35

当你与对方握手时,使用的PeerID应该是你的,而不是对方的。

因此,您的消息应如下所示:

代码语言:javascript
复制
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。

票数 1
EN

Stack Overflow用户

发布于 2014-02-01 03:08:20

我不会写红宝石。这是python代码:

代码语言:javascript
复制
send_string = chr(19)+"BitTorrent Protocol"+8*chr(0)+info_hash+peer_id
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18394049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档