# C++ #ND2 challenge wrong MessageID and wrong index begin

**URL:** <https://forum.codecrafters.io/t/c-nd2-challenge-wrong-messageid-and-wrong-index-begin/2622>\
**Category:** Challenges\
**Tags:** challenge:bittorrent\
**Created:** [October 27, 2024, 1:28pm UTC](https://forum.codecrafters.io/t/c-nd2-challenge-wrong-messageid-and-wrong-index-begin/2622 "2024-10-27T13:28:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ycsvenom](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/ycsvenom/32/2609_2.png) [@ycsvenom](https://forum.codecrafters.io/u/ycsvenom)\
**Post date:** [October 27, 2024, 1:28pm UTC](https://forum.codecrafters.io/t/c-nd2-challenge-wrong-messageid-and-wrong-index-begin/2622/1 "2024-10-27T13:28:51Z")

</div>

I’m stuck on Stage #ND2 C++

I have tested everything but I keep getting wrong hashes, and my first `PIECE` ID is indeed 7 but the rest is -32 and the returned `index`, `begin` are gibberish from the first response  
Anyone can help?

Here are my logs:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/c/c1fe0687e137b07d2a42c2080f6d00c3a3986a16.png)

Here’s my code

```cpp
void TorrentClient::downloadPiece(const std::string &path, size_t pieceIndex) const
{
	std::ofstream out(path, std::ios_base::out);

	if (!out) {
		std::stringstream error;
		error << "Error creating file: " << path << std::endl;
		exit_with_message(error);
	}

	auto message = m_client->recv();
	if (!message.isType(PeerMessagesType::BITFIELD))
		exit_with_message(make_message(message));

	m_client->send(PeerMessage(PeerMessagesType::INTERESTED));
	
	message = m_client->recv();
	if (!message.isType(PeerMessagesType::UNCHOKE))
		exit_with_message(make_message(message));

	size_t size = m_torrent.piece_length;
	size_t blockIndex = 0;

	while (size > 0) {
		size_t blockSize = size > BLOCK_SIZE ? BLOCK_SIZE : size;
		std::stringstream ss;
		ss << bitos(pieceIndex)
		   << bitos(BLOCK_SIZE * (blockIndex++))
		   << bitos(blockSize);
		m_client->send(PeerMessage(PeerMessagesType::REQUEST, ss.str()));
		
		message = m_client->recv();
		// if (!message.isType(PeerMessagesType::PIECE))
		// exit_with_message(make_message(message));

		auto payload = std::string_view(message.getPayload());

		[[maybe_unused]]
		auto index = bstoi(std::string(payload.substr(0, sizeof(int))));
		payload.remove_prefix(sizeof(int));

		[[maybe_unused]]
		auto begin = bstoi(std::string(payload.substr(0, sizeof(int))));
		payload.remove_prefix(sizeof(int));

		auto block = payload;

		out << block;
		size -= block.size();
	}
}

```

NOTE: the receive and send method is tested out effectively where they ensure sending and receiving the whole message regardless of the size. also the maximum buffer size is 32 KB which is twice as big as block size

---

<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:** [October 27, 2024, 2:23pm UTC](https://forum.codecrafters.io/t/c-nd2-challenge-wrong-messageid-and-wrong-index-begin/2622/2 "2024-10-27T14:23:04Z")

</div>

Could you [upload your code to GitHub](https://docs.codecrafters.io/features/publish-to-github) and share the link? It will be much easier to debug if I can run it directly.

I may need to get back to you in a few days.

---

<div class="post-metadata">

**Author:** ![ycsvenom](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/ycsvenom/32/2609_2.png) [@ycsvenom](https://forum.codecrafters.io/u/ycsvenom)\
**Post date:** [October 27, 2024, 2:54pm UTC](https://forum.codecrafters.io/t/c-nd2-challenge-wrong-messageid-and-wrong-index-begin/2622/4 "2024-10-27T14:54:43Z")

</div>

The code is already published on [github](https://github.com/ycsvenom/bittorrent), I just have included my last version, I will be more than happy to hear some comments about the code from you, Thanks

---

<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:** [October 28, 2024, 12:20pm UTC](https://forum.codecrafters.io/t/c-nd2-challenge-wrong-messageid-and-wrong-index-begin/2622/5 "2024-10-28T12:20:18Z")

</div>

@ycsvenom I added several logs to your code and here’s what I found:

1. Your code was sending and receiving messages accurately.  
a. Messages sizes were correct, with **16392 = 1 + 4 + 4 + 16k** bytes.  
b. Messages received in `socket.cpp` were consistent, with **0x4000** increments ( **16k** in decimal).

2. However, some messages became garbled when transferred from `socket.cpp` to `peer_socket_wrapper`. This may be worth investigating to see why the data isn’t intact at that stage.

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/5/5e8f64bda0090fce1cdc1f56201f43b8228bd883.jpeg)

---

<div class="post-metadata">

**Author:** ![ycsvenom](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/ycsvenom/32/2609_2.png) [@ycsvenom](https://forum.codecrafters.io/u/ycsvenom)\
**Post date:** [October 28, 2024, 12:42pm UTC](https://forum.codecrafters.io/t/c-nd2-challenge-wrong-messageid-and-wrong-index-begin/2622/6 "2024-10-28T12:42:34Z")

</div>

@andy1li it’s weird indeed, I have tested socket wrapper in the tests directory, it sends and receives the exact replica of the content, I will investigate further on this,  
If you read the code, do you have any suggestions or notes?

Thanks for help

---

<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:** [October 28, 2024, 12:50pm UTC](https://forum.codecrafters.io/t/c-nd2-challenge-wrong-messageid-and-wrong-index-begin/2622/7 "2024-10-28T12:50:34Z")

</div>

@ycsvenom Feel free to tag me again if you’re still stuck after a few days. Honestly, my C++ skills are a bit rusty, but I’ll do my best to help!

---

<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:** [November 4, 2024, 12:38am UTC](https://forum.codecrafters.io/t/c-nd2-challenge-wrong-messageid-and-wrong-index-begin/2622/8 "2024-11-04T00:38:16Z")

</div>

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

---

<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:** [November 9, 2024, 12:39am UTC](https://forum.codecrafters.io/t/c-nd2-challenge-wrong-messageid-and-wrong-index-begin/2622/9 "2024-11-09T00:39:04Z")

</div>

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