# Bittorrent, peer handshake response is painfully slow. #ca4

**URL:** <https://forum.codecrafters.io/t/bittorrent-peer-handshake-response-is-painfully-slow-ca4/12567>\
**Category:** Challenges\
**Tags:** challenge:bittorrent\
**Created:** [June 24, 2025, 8:23am UTC](https://forum.codecrafters.io/t/bittorrent-peer-handshake-response-is-painfully-slow-ca4/12567 "2025-06-24T08:23:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Direwolfesp](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/direwolfesp/32/9232_2.png) [@Direwolfesp](https://forum.codecrafters.io/u/Direwolfesp)\
**Post date:** [June 24, 2025, 8:23am UTC](https://forum.codecrafters.io/t/bittorrent-peer-handshake-response-is-painfully-slow-ca4/12567/1 "2025-06-24T08:23:42Z")

</div>

Hi, I already completed the Peer Handshake stage however I noticed I have to wait around 2 minutes for the peer to response.  
Is this some normal behaviour or am I doing something wrong?

pd: im doing it in zig and these are the logs from my program

```plaintext
> timeit { zbr -- handshake sample.torrent 165.232.38.164:51433 }
info: Parsed file sample.torrent
info: Created handshake struct
info: Peer ip: 165.232.38.164:51433
info: Trying to connect to peer...
info: Connected to peer
info: Sending handshake to peer...
info: Waiting for response...
info: Got a response from peer
info: Peer ID: 2d524e302e302e302d9d5e0226875575c6233a13
2min 108ms 115µs 598ns

```

and here is a [link](https://github.com/Direwolfesp/zigtorrent/blob/6f46f83ff7c23fcf37075a83e9b890d63deec78a/src/main.zig#L96) to the source code from github.

---

<div class="post-metadata">

**Author:** ![Direwolfesp](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/direwolfesp/32/9232_2.png) [@Direwolfesp](https://forum.codecrafters.io/u/Direwolfesp)\
**Post date:** [June 24, 2025, 8:37am UTC](https://forum.codecrafters.io/t/bittorrent-peer-handshake-response-is-painfully-slow-ca4/12567/4 "2025-06-24T08:37:31Z")

</div>

UPDATE: Okai so I looked for some other people solution and converted the handshake `struct` into a `extern struct` and just used `try writer.writeStruct(handshake);` and `try reader.readStruct(HandShake)` and the times went to 75ms which good but i have no idea why does this happen??

---

<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:** [June 24, 2025, 8:42am UTC](https://forum.codecrafters.io/t/bittorrent-peer-handshake-response-is-painfully-slow-ca4/12567/5 "2025-06-24T08:42:01Z")

</div>

Hey @Direwolfesp, ming sharing what you’re using before the conversion?

---

<div class="post-metadata">

**Author:** ![Direwolfesp](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/direwolfesp/32/9232_2.png) [@Direwolfesp](https://forum.codecrafters.io/u/Direwolfesp)\
**Post date:** [June 24, 2025, 8:49am UTC](https://forum.codecrafters.io/t/bittorrent-peer-handshake-response-is-painfully-slow-ca4/12567/6 "2025-06-24T08:49:29Z")

</div>

Sure,  
before:

```zig
.handshake => {
            // . . . . .

            std.log.info("Trying to connect to peer...", .{});
            var connection = try std.net.tcpConnectToAddress(addr);
            std.log.info("Connected to peer", .{});
            const writer = connection.writer();
            const reader = connection.reader();

            std.log.info("Sending handshake to peer...", .{});
            try handshake.dumpToWriter(writer);
            std.log.info("Waiting for response...", .{});
            const response: []u8 = try reader.readAllAlloc(allocator, std.math.maxInt(usize));
            defer allocator.free(response);
            std.log.info("Got a response from peer ", .{});
            const resp_handshake = HandShake.createFromBuffer(response);
            const peer_id = std.fmt.fmtSliceHexLower(&resp_handshake.peer_id);
            std.log.info("Peer ID: {s}", .{peer_id});
        },

```

after (correct):

```zig
        .handshake => {
            // . . . . .

            // connect to peer
            std.log.info("Trying to connect to peer...", .{});
            var connection = try std.net.tcpConnectToAddress(addr);
            std.log.info("Connected to peer", .{});
            const writer = connection.writer();
            const reader = connection.reader();

            // send and receive handshake
            std.log.info("Sending handshake to peer...", .{});
            try writer.writeStruct(handshake);
            std.log.info("Waiting for response...", .{});
            const resp_handshake = try reader.readStruct(HandShake);
            std.log.info("Got a response from peer ", .{});
            const peer_id = std.fmt.fmtSliceHexLower(&resp_handshake.peer_id);
            std.log.info("Peer ID: {s}", .{peer_id});

```

---

<div class="post-metadata">

**Author:** ![Direwolfesp](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/direwolfesp/32/9232_2.png) [@Direwolfesp](https://forum.codecrafters.io/u/Direwolfesp)\
**Post date:** [June 24, 2025, 8:51am UTC](https://forum.codecrafters.io/t/bittorrent-peer-handshake-response-is-painfully-slow-ca4/12567/7 "2025-06-24T08:51:30Z")

</div>

also declared the handshake as a extern struct:

```zig
pub const HandShake = extern struct {
    // layout matters
    pstrlen: u8 align(1) = 19,
    pstr: [19]u8 align(1) = "BitTorrent protocol".*,
    reserved: [8]u8 align(1) = std.mem.zeroes([8]u8),
    info_hash: [20]u8 align(1) = undefined,
    peer_id: [20]u8 align(1) = "-qB6666-weoiuv8324ns".*,
  // . . . . .
};

```

---

<div class="post-metadata">

**Author:** ![Direwolfesp](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/direwolfesp/32/9232_2.png) [@Direwolfesp](https://forum.codecrafters.io/u/Direwolfesp)\
**Post date:** [June 24, 2025, 9:06am UTC](https://forum.codecrafters.io/t/bittorrent-peer-handshake-response-is-painfully-slow-ca4/12567/8 "2025-06-24T09:06:35Z")

</div>

Okai, i also tried using:

```zig
var a = try reader.readBytesNoEof(@sizeOf(HandShake));
const resp_handshake = HandShake.createFromBuffer(&a);

```

and seems to also work, so the problem had to be:

```zig
const response: []u8 = try reader.readAllAlloc(allocator, std.math.maxInt(usize));

```

which might be allocating to many wasted memory.

---

<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:** [June 24, 2025, 3:56pm UTC](https://forum.codecrafters.io/t/bittorrent-peer-handshake-response-is-painfully-slow-ca4/12567/9 "2025-06-24T15:56:22Z")

</div>

@Direwolfesp `readAllAlloc` internally calls `readAllArrayList`, which reads until the end of the stream. This behavior makes it unsuitable for use with TCP streams.

Since TCP streams remain open until explicitly closed by the peer, this approach can block indefinitely or lead to unexpected hangs.

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

---

<div class="post-metadata">

**Author:** ![Direwolfesp](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/direwolfesp/32/9232_2.png) [@Direwolfesp](https://forum.codecrafters.io/u/Direwolfesp)\
**Post date:** [June 24, 2025, 4:23pm UTC](https://forum.codecrafters.io/t/bittorrent-peer-handshake-response-is-painfully-slow-ca4/12567/11 "2025-06-24T16:23:47Z")

</div>

oh that has sense, thank you very much ^^

---

<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:** [June 29, 2025, 4:24pm UTC](https://forum.codecrafters.io/t/bittorrent-peer-handshake-response-is-painfully-slow-ca4/12567/12 "2025-06-29T16:24:25Z")

</div>

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