-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Closed
Milestone
Description
The most simple server echo I can think of transfers less than 1MB/s. It doesn't matter if it's evented or not or if using streams or not.
// To use:
// 1. Start server with "zig build run"
// 2.Then test it with "time dd if=/dev/zero | nc -v -v -n 127.0.0.1 9001"
// 3. Press ctrl+c and nwill output the speed
const std = @import("std");
const net = std.net;
pub const io_mode = .evented;
pub fn main() anyerror!void {
const allocator = std.heap.direct_allocator;
const req_listen_addr = try net.Address.parseIp4("127.0.0.1", 9001);
std.event.Loop.instance.?.beginOneEvent();
var server = net.StreamServer.init(.{});
defer server.deinit();
try server.listen(req_listen_addr);
std.debug.warn("listening at {}\n", server.listen_address);
while (true) {
const conn = try server.accept();
std.debug.warn("connected to {}\n", conn.address);
echo(conn) catch |err| {
std.debug.warn("connection dropped {}\n", conn.address);
};
}
}
pub fn echo(conn: net.StreamServer.Connection) !void {
var in_stream = &conn.file.inStream().stream;
var out_stream = &conn.file.outStream().stream;
while (true) {
var c: u8 = try in_stream.readByte();
try out_stream.writeByte(c);
}
}
dd if=/dev/zero | nc -v -v -n 127.0.0.1 9001
Connection to 127.0.0.1 9001 port [tcp/*] succeeded!
^C17529+0 records in
17528+0 records out
8974336 bytes (9.0 MB, 8.6 MiB) copied, 17.2288 s, 521 kB/s
Also says 104 is an unexpected errno when aborting.
Metadata
Metadata
Assignees
Labels
No labels