# \#ND2 - Not receiving unchoke message

**URL:** <https://forum.codecrafters.io/t/nd2-not-receiving-unchoke-message/4413>\
**Category:** Challenges\
**Tags:** challenge:bittorrent\
**Created:** [January 23, 2025, 10:21am UTC](https://forum.codecrafters.io/t/nd2-not-receiving-unchoke-message/4413 "2025-01-23T10:21:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eaverdeja](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/eaverdeja/32/4167_2.png) [@eaverdeja](https://forum.codecrafters.io/u/eaverdeja)\
**Post date:** [January 23, 2025, 10:21am UTC](https://forum.codecrafters.io/t/nd2-not-receiving-unchoke-message/4413/1 "2025-01-23T10:21:20Z")

</div>

Hey folks. I’m stuck on #ND2 and would like some help.

I’m currently on the step for receiving the `unchoke` message, right after sending the `interested` message. I’m not getting any data back from the peer though, and tests time out.

FYI I’m doing a sequential implementation for starters.

Here’s the relevant piece of code:

```python
            peers = get_peers(torrent_filename)
            peer_id, reader, writer = await perform_handshake(
                torrent_filename, peer=peers[0]
            )

            # Receive bitfield message
            bitfield_size = int.from_bytes(await reader.readexactly(4))
            bitfield_type = int.from_bytes(await reader.readexactly(1))
            if bitfield_type != 0x05:
                raise Exception("Expected bitfield type")
            _bitfield_payload = await reader.read(bitfield_size)

            # Send interested message
            # Payload size is 0, interested type is 2
            message = bytes().join(int.to_bytes(0) for _ in range(4)) + int.to_bytes(2)
            writer.write(message)
            await writer.drain()

            # Receive unchoke message
            unchoke_size = int.from_bytes(await reader.readexactly(4))
            unchoke_type = int.from_bytes(await reader.readexactly(1))
            if unchoke_type != 0x01 or unchoke_size != 0x00:
                raise Exception("Expected unchoke type with 0 size")
            print("foo!")

```

And here is the repo with all of the code:

> **[GitHub - eaverdeja/codecrafters-bittorrent-python: https://app.codecrafters.io/courses/bittorrent](https://github.com/eaverdeja/codecrafters-bittorrent-python)**
>
> https://app.codecrafters.io/courses/bittorrent

---

<div class="post-metadata">

**Author:** ![eaverdeja](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/eaverdeja/32/4167_2.png) [@eaverdeja](https://forum.codecrafters.io/u/eaverdeja)\
**Post date:** [January 23, 2025, 10:47am UTC](https://forum.codecrafters.io/t/nd2-not-receiving-unchoke-message/4413/2 "2025-01-23T10:47:11Z")

</div>

I figured it out by looking at some code examples:

The “message length prefix” for the `interested` message should be 1, not 0, as it should include the type byte itself, not just the payload.

With a message length prefix of 1 I was able to continue and receive the unchoke message 🎉

---

<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:** [January 28, 2025, 10:47am UTC](https://forum.codecrafters.io/t/nd2-not-receiving-unchoke-message/4413/3 "2025-01-28T10:47:20Z")

</div>

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