Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
// This is the original file written by andrewrk in his chat demo // on youtube const std = @import("std"); const net = std.net; const fs = std.fs; const os = std.os; 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", 9000); 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); var room = Room{ .clients = std.AutoHashMap(*Client, void).init(allocator), .lock = std.Mutex.init(), }; while (true) { const conn = try server.accept(); const client = try allocator.create(Client); client.* = Client{ .file = conn.file, .address = conn.address, .handle_frame = async client.handle(&room), }; var held_lock = room.lock.acquire(); defer held_lock.release(); try room.clients.putNoClobber(client, {}); } } const Client = struct { file: fs.File, address: net.Address, handle_frame: @Frame(handle), fn handle(self: *Client, room: *Room) !void { var stream = self.file.inStream().stream; std.debug.warn("{}\n", self.address); try self.file.write("server: welcome to teh chat server \n"); //try self.file.write(self.address.toString()); //try stream.print("{}\n", self.address); while (true) { var buf: [100]u8 = undefined; const amt = try stream.read(&buf); const msg = buf[0..amt]; try room.broadcast(msg, self); } } }; const Room = struct { clients: std.AutoHashMap(*Client, void), lock: std.Mutex, fn broadcast(room: *Room, msg: []const u8, sender: *Client) !void { var held_lock = room.lock.acquire(); defer held_lock.release(); var it = room.clients.iterator(); while (it.next()) |entry| { const client = entry.key; if (client == sender) continue; const fd = client.file.handle; _ = try os.send(fd, msg, os.MSG_NOSIGNAL); } } };
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree