Skip to content

Instantly share code, notes, and snippets.

@lithdew
Created June 7, 2020 16:54
Show Gist options
  • Save lithdew/c39d72c69beb455011da9c7ab236f844 to your computer and use it in GitHub Desktop.
Save lithdew/c39d72c69beb455011da9c7ab236f844 to your computer and use it in GitHub Desktop.
`zig-network` test.
const std = @import("std");
const net = @import("./zig-network/network.zig");
const PROTOCOL = net.Protocol.tcp;
pub fn main() !void {
var buf: [16384]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buf);
try net.init();
defer net.deinit();
const sock = try net.connectToHost(&fba.allocator, "localhost", 9000, PROTOCOL);
defer sock.close();
const stream = sock.outStream();
const data = try fba.allocator.create([1000]u8);
defer fba.allocator.destroy(data);
var rng = std.rand.DefaultCsprng.init(0);
rng.random.bytes(data);
while (true) {
_ = try stream.writeAll(data);
}
}
const std = @import("std");
const net = @import("./zig-network/network.zig");
const PROTOCOL = net.Protocol.tcp;
pub fn main() !void {
var fb: [16384]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&fb);
const buf = try fba.allocator.create([1000]u8);
defer fba.allocator.free(buf);
try net.init();
defer net.deinit();
const sock = try net.Socket.create(net.AddressFamily.ipv4, PROTOCOL);
defer sock.close();
try sock.bindToPort(9000);
switch (PROTOCOL) {
.udp => {
while (true) {
_ = try sock.inStream().readAll(buf);
}
},
.tcp => {
try sock.listen();
const client = try sock.accept();
while (true) {
_ = try client.inStream().readAll(buf);
}
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment