Created
October 3, 2018 08:42
-
-
Save bkleiner/22bb6d1b18946201c7b55844d98de113 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const net = std.event.net; | |
const warn = std.debug.warn; | |
const request = "GET / HTTP/1.1\r\nHost: localhost:8000\r\n\r\n"; | |
async fn asyncClient(loop: *std.event.Loop, address: *const std.net.Address) error!void { | |
errdefer @panic("asyncClient"); | |
warn("connecting\n"); | |
var file = try await try async net.connect(loop, address); | |
defer file.close(); | |
warn("connected\n"); | |
warn("writeing\n"); | |
try await try async net.write(loop, file.handle, request[0..]); | |
warn("wrote {}\n", request.len); | |
warn("reading\n"); | |
var in: [512]u8 = undefined; | |
var len = try await try async net.read(loop, file.handle, in[0..]); | |
warn("read {} {}\n", len, in[0..len]); | |
} | |
pub fn main() !void { | |
warn("main start\n"); | |
const ip4addr = std.net.parseIp4("127.0.0.1") catch unreachable; | |
const addr = std.net.Address.initIp4(ip4addr, 8000); | |
var loop: std.event.Loop = undefined; | |
try loop.initSingleThreaded(std.debug.global_allocator); | |
const p = try async<std.debug.global_allocator> asyncClient(&loop, addr); | |
defer cancel p; | |
warn("loop run\n"); | |
loop.run(); | |
warn("loop end\n"); | |
warn("main end\n"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment