What lib (net, os, posix) should I use to make a socket in zig 0.15.1

I want to write a http server in zig and I am confused what lib should I use abd how to make a socket.?

Either should it net, os or posix.

Kindly ignore I am missing things. I am new to zig and want to explore this and having a little hard-time because I am coming from python backend development background.

So kindly help.

net that is the cross-platform networking API.

posix is a POSIX compatible API, with limited support for windows.
It is lower level than net, fs, etc.

os has a very short list of cross-platform abstractions which seem to be in limbo, but provides access to submodules for each supported OS e.g. std.os.linux

4 Likes

so for socket, I should use net.?

yes, unless you want to use lower level things such as iouring, kqueue, etc to take advantage of nonblocking io. in that case you would use the OS specific modules.

I want to make a http server and I believe that lower-level things would be better for learning and clarity. This would help me to learn a lot of things about zig.

As a source of information for your endeavor, please check GitHub - lalinsky/zio: Async I/O framework for Zig and its sublibs. @lalinsky is also working in a http server at GitHub - lalinsky/dusty: HTTP server library for Zig

3 Likes

does it mean that for linux non-blocked sockets in 0.15.+ we will use the same

posix api?

Be careful here. You are attempting to learn 2 separate (albeit overlapping) domains. Low level things, and Zig.

I would suggest picking one at a time. If your main focus is to get better at Zig (including Allocators, Io), I would suggest sticking to std.net and use the provided higher level abstractions.

That’s not to say that you can’t do both at the same time, just be aware that this will increase the cognitive load as you try to learn specific underlying implementations on top of the language.

4 Likes

Not an answer to your question but as a tip, there’s a http server and client implementation in std.http, which will probably answer a lot of your “How do I do X in Zig?” questions.

2 Likes