# Cannot receive data after magnet handshake

**URL:** <https://forum.codecrafters.io/t/cannot-receive-data-after-magnet-handshake/4782>\
**Category:** Challenges\
**Tags:** challenge:bittorrent\
**Created:** [February 10, 2025, 6:23am UTC](https://forum.codecrafters.io/t/cannot-receive-data-after-magnet-handshake/4782 "2025-02-10T06:23:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![silversword05](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/silversword05/32/3123_2.png) [@silversword05](https://forum.codecrafters.io/u/silversword05)\
**Post date:** [February 10, 2025, 6:23am UTC](https://forum.codecrafters.io/t/cannot-receive-data-after-magnet-handshake/4782/1 "2025-02-10T06:23:18Z")

</div>

I’m stuck on Stage #QV6.  
It seems that the data request message is not receiving any response.  
The code is waiting on read for a long time until it fails.

Here are my logs:

```bash
$ ./your_bittorrent.sh magnet_download_piece -o /tmp/test-piece-0 "magnet:?xt=urn:btih:c5fb9894bdaba464811b088d806bdd611ba490af&dn=magnet3.gif&tr=http%3A%2F%2Fbittorrent-test-tracker.codecrafters.io%2Fannounce" 0
-- Running vcpkg install
All requested packages are currently installed.
Total install time: 991 ns
-- Running vcpkg install - done
-- Configuring done (1.3s)
-- Generating done (0.4s)
-- Build files have been written to: /mnt/d/CodeCrafters/codecrafters-bittorrent-cpp/build
[14%] Building CXX object CMakeFiles/bittorrent.dir/src/Commands.cc.o
[28%] Linking CXX executable bittorrent
[100%] Built target bittorrent
Sending data (68 bytes):
00000000: 13 42 69 74 54 6f 72 72 65 6e 74 20 70 72 6f 74 .BitTorrent prot
00000010: 6f 63 6f 6c 00 00 00 00 00 10 00 00 c5 fb 98 94 ocol............
00000020: bd ab a4 64 81 1b 08 8d 80 6b dd 61 1b a4 90 af ...d.....k.a....
00000030: 2d 54 52 32 39 34 30 2d 30 62 30 62 30 62 30 62 -TR2940-0b0b0b0b
00000040: 30 62 30 62 0b0b
Handshake read (68 bytes):
00000000: 13 42 69 74 54 6f 72 72 65 6e 74 20 70 72 6f 74 .BitTorrent prot
00000010: 6f 63 6f 6c 00 00 00 00 00 10 00 04 c5 fb 98 94 ocol............
00000020: bd ab a4 64 81 1b 08 8d 80 6b dd 61 1b a4 90 af ...d.....k.a....
00000030: 2d 52 4e 30 2e 30 2e 30 2d e3 3d b7 66 6c 49 ec -RN0.0.0-.=.flI.
00000040: 50 4f fd cb PO..
Message read (6 bytes):
00000000: 00 00 00 02 05 e0 ......
Message read (103 bytes):
00000000: 00 00 00 63 14 00 64 31 3a 6d 64 31 31 3a 75 74 ...c..d1:md11:ut
00000010: 5f 6d 65 74 61 64 61 74 61 69 31 65 36 3a 75 74 _metadatai1e6:ut
00000020: 5f 70 65 78 69 32 65 65 31 33 3a 6d 65 74 61 64 _pexi2ee13:metad
00000030: 61 74 61 5f 73 69 7a 65 69 31 33 32 65 34 3a 72 ata_sizei132e4:r
00000040: 65 71 71 69 32 35 30 65 31 3a 76 31 30 3a 52 61 eqqi250e1:v10:Ra
00000050: 69 6e 20 30 2e 30 2e 30 36 3a 79 6f 75 72 69 70 in 0.0.06:yourip
00000060: 34 3a a2 c8 6c b3 65 4:..l.e
Sending data (30 bytes):
00000000: 00 00 00 1a 14 00 64 31 3a 6d 64 31 31 3a 75 74 ......d1:md11:ut
00000010: 5f 6d 65 74 61 64 61 74 61 69 31 65 65 65 _metadatai1eee
Sending data (31 bytes):
00000000: 00 00 00 1b 14 01 64 38 3a 6d 73 67 5f 74 79 70 ......d8:msg_typ
00000010: 65 69 30 65 35 3a 70 69 65 63 65 69 30 65 65 ei0e5:piecei0ee
Message read (181 bytes):
00000000: 00 00 00 b1 14 01 64 38 3a 6d 73 67 5f 74 79 70 ......d8:msg_typ
00000010: 65 69 31 65 35 3a 70 69 65 63 65 69 30 65 31 30 ei1e5:piecei0e10
00000020: 3a 74 6f 74 61 6c 5f 73 69 7a 65 69 31 33 32 65 :total_sizei132e
00000030: 65 64 36 3a 6c 65 6e 67 74 68 69 36 32 39 39 34 ed6:lengthi62994
00000040: 34 65 34 3a 6e 61 6d 65 31 31 3a 6d 61 67 6e 65 4e4:name11:magne
00000050: 74 33 2e 67 69 66 31 32 3a 70 69 65 63 65 20 6c t3.gif12:piece l
00000060: 65 6e 67 74 68 69 32 36 32 31 34 34 65 36 3a 70 engthi262144e6:p
00000070: 69 65 63 65 73 36 30 3a ca 80 fd 83 ff b3 4d 6e ieces60:......Mn
00000080: 1b bd 26 a8 ef 6d 30 58 27 f1 cd 0a 70 7f d7 c6 ..&..m0X'...p...
00000090: 57 f6 d6 36 f0 58 34 66 c3 cf e1 34 dd b2 c0 8a W..6.X4f...4....
000000a0: 47 07 6d 10 4d 21 4c 00 52 96 0e f7 67 26 26 49 G.m.M!L.R...g&&I
000000b0: a8 af 0e a8 65 ....e
Sending data (17 bytes):
00000000: 0d 00 00 00 06 00 00 00 00 00 00 00 00 00 00 40 ...............@
00000010: 00 
....
bittorrent: /mnt/d/CodeCrafters/codecrafters-bittorrent-cpp/src/TCPHandler.cc:80: std::string TCPHandler::readMessage() const: Assertion `("Invalid message length", message_length > 0 && message_length <= (1 << 14) + 10)' failed.
Aborted (core dumped)

```

And here’s a snippet of my code:

```cpp
void Commands::downloadMagentPiece(const std::string &magnet_link,
                                   const std::string &output_file_path,
                                   const uint32_t piece_index) {
    // std::cerr << __PRETTY_FUNCTION__ << std::endl;

    auto [peers, info_hash] = getTCPHandlerAndInfoHash(magnet_link);
    auto tcp_handler = std::make_unique<TCPHandler>(peers[1]);

    json info = getMagnentLinkInfo(*tcp_handler, info_hash);

    PieceDownloader piece_downloader(info, std::move(tcp_handler));
    std::string piece_data = piece_downloader.downloadPiece(piece_index);

    std::ofstream output_file(output_file_path, std::ios::binary);
    output_file.write(piece_data.data(), piece_data.size());

    std::string pieces_val = info["pieces"].get<std::string>();
    assert(("Invalid pieces value size", pieces_val.size() % 20 == 0));
    std::string piece_hash = pieces_val.substr(piece_index * 20, 20);
    assert(("Piece verification failed", verifyPeice(piece_data, piece_hash)));

    std::cerr << "Piece downloaded successfully" << std::endl;
}

// Detailed code: https://github.com/silversword05/codecrafters-bittorrent-cpp

```

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [February 10, 2025, 12:33pm UTC](https://forum.codecrafters.io/t/cannot-receive-data-after-magnet-handshake/4782/3 "2025-02-10T12:33:55Z")

</div>

Hi @silversword05, could you confirm if this is the same issue as [this thread](https://forum.codecrafters.io/t/error-in-order-of-extension-handshake-messages-and-wrong-byte-order-in-length-field-of-extension-handshake-request-message/4778), or has that been resolved?

---

<div class="post-metadata">

**Author:** ![silversword05](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/silversword05/32/3123_2.png) [@silversword05](https://forum.codecrafters.io/u/silversword05)\
**Post date:** [February 24, 2025, 2:57am UTC](https://forum.codecrafters.io/t/cannot-receive-data-after-magnet-handshake/4782/4 "2025-02-24T02:57:19Z")

</div>

No it is a separate issue. Sorry for the late reply.

---

<div class="post-metadata">

**Author:** ![silversword05](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/silversword05/32/3123_2.png) [@silversword05](https://forum.codecrafters.io/u/silversword05)\
**Post date:** [February 24, 2025, 3:32am UTC](https://forum.codecrafters.io/t/cannot-receive-data-after-magnet-handshake/4782/5 "2025-02-24T03:32:21Z")

</div>

Can I get the github link to the testing code or complete code example? The code examples are only the git diffs which are not sufficient.

---

<div class="post-metadata">

**Author:** ![silversword05](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/silversword05/32/3123_2.png) [@silversword05](https://forum.codecrafters.io/u/silversword05)\
**Post date:** [February 24, 2025, 3:44am UTC](https://forum.codecrafters.io/t/cannot-receive-data-after-magnet-handshake/4782/6 "2025-02-24T03:44:55Z")

</div>

This is resolved. For context, we have to redo the base handshake before downloading the pieces.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [February 26, 2025, 6:49am UTC](https://forum.codecrafters.io/t/cannot-receive-data-after-magnet-handshake/4782/7 "2025-02-26T06:49:11Z")

</div>

@silversword05 You can find the tester code here:

> **[bittorrent-tester/internal at main · codecrafters-io/bittorrent-tester](https://github.com/codecrafters-io/bittorrent-tester/tree/main/internal)**
>
> Tester for the "Build your own BitTorrent" challenge - codecrafters-io/bittorrent-tester

Unfortunately, we don’t currently have complete code examples available.

I’ll share your feedback with the team, and we’ll be discussing ways to improve your experience.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [March 3, 2025, 10:27am UTC](https://forum.codecrafters.io/t/cannot-receive-data-after-magnet-handshake/4782/9 "2025-03-03T10:27:21Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
