我正在開發使用C++的torrent客戶端。 我無法理解消息與對等體的結構。 如何使用C++創建簡單的握手消息,以及如何讀取響應? 問題是我必須發送的數據的結構,然後是我必須閱讀的結構。 我想向種子發送一個握手消息,例如發送一個BlockBuf。 我如何創建BlockBuf的內容? 問題是結構我必須使用用於消息和不等連接對等.... :(C++中的BitTorrent Peer Wire消息
2
A
回答
6
所以BitTorrent handshake是由,爲了:
- 與值中的字節19(後面的字符串的長度);
- 的UTF-8字符串「BitTorrent協議」(這是與在ASCII); 用來標記擴展
- 8個保留字節;
- 20個字節的洪流信息哈希;
- 對等體ID的20個字節。
所以,你可以通過獲得大的緩衝區足夠握手消息開始:
const int handshake_size = 1+19+8+20+20;
char handshake[handshake_size];
計算偏移事先也有助於:
const int protocol_name_offset = 1;
const int reserved_offset = protocol_name_offset + 19;
const int info_hash_offset = reserved_offset + 8;
const int peer_id_offset = info_hash_offset + 20;
然後你只需要填寫它起來。
const char prefix = 19;
const std::string BitTorrent_protocol = "BitTorrent protocol";
handshake[0] = prefix; // length prefix of the string
std::copy(BitTorrent_protocol.begin(), BitTorrent_protocol.end(),
&handshake[protocol_name_offset]); // protocol name
等等其餘的數據。
然後,可以將緩衝區直接發送到您將使用的任何網絡API。
要閱讀的答覆,您提取緩衝的部分,並相應確認:
if(reply[0] != prefix) {
// fail
}
if(!std::equal(BitTorrent_protocol.begin(), BitTorrent_protocol.end(), &reply[protocol_name_offset]) {
// fail
}
等。
不建議直接從網絡讀取和寫入結構,因爲您需要完全控制佈局,否則信息將變形。
相關問題
- 1. BitTorrent Peer Handshake
- 2. BitTorrent協議實現:未收到「Piece」消息
- 3. Live Peer Peer Swarm
- 4. Bittorrent:爲什麼peer字段的值是二進制的,而不是Bencoded列表?
- 5. C socket - recv:connection reset by peer
- 6. c#中的TVN_SELCHANGING消息#
- 7. Asp.NET中的消息框C#
- 8. Objective-C的消息
- 9. BitTorrent擴展協議
- 10. protobuf:從C#讀取C++中的消息
- 11. 在c中顯示消息#
- 12. C#消息框錯誤消息
- 13. 什麼意思是在Bittorrent的握手信息之前的長字節流?
- 14. Bittorrent:響應消息是什麼「???? ackeConnection ID mismatch。」從跟蹤服務器的意思?
- 15. 在C++中爲消息泵定義自己的WM消息
- 16. nodejs中的bittorrent tracker播種器和leecher
- 17. Linux中C中的消息框
- 18. silverlight中的bittorrent客戶端
- 19. 爲什麼對等方發送握手消息來響應我發送的握手消息?
- 20. BitTorrent對等線協議(TCP)
- 21. C#即時消息
- 22. 消息框在C#
- 23. C#錯誤消息
- 24. C#DLL消息框
- 25. Bittorrent連接ID不匹配
- 26. 獲取Bittorrent中的IP地址
- 27. C++消息傳遞的疑惑
- 28. 聽OS的消息在C#
- 29. 用於C++的MSBuild消息
- 30. 的Visual C#.NET消息框
有多個問題你問,沒有顯示任何代碼,定義爲什麼這是一個問題,請張貼您的代碼。 – EdChum 2012-03-23 09:59:00
我正在開發torrent應用,而我在軍隊。現在我有一天休息,我沒有開發計算機的家,所以我沒有在可用的代碼code.Also我沒有訪問互聯網,而我在軍隊!:P所以我認爲這是一個僵局,我不能發佈任何代碼!主要問題是用C++編寫的握手消息的例子。 – nikosdi 2012-03-23 10:04:12
BitTorrent規範的哪一部分不清楚?對等消息具有相當簡單的結構 - 定義一個類來保存數據並從中生成一個流並不困難。 – 2012-03-23 10:09:12