Networking newb problems (how to accept clients in a nonblocking way)

I want to have an (ideally) single threaded server, that can accept clients without blocking. Is there a way to check if a client wants to connect, or am I forced to go multithreaded?
Same question for reading what a client is sending btw

1 Like

Hi! These might be promising solutions to explore:

  1. libxev – cross-platform event loop.

  2. http.zig – the only HTTP server in Zig that has non-blocking support (Unix-only).

1 Like

Thank you, is one of these is tcp? I forgot to mention that im making a tcp server

1 Like

Yeah, libxev has a TCP client/server.

1 Like

Ah okay thank you

Yikes ive updated zig and now i dont know how to make a server lol

1 Like

If anyone knows how to please tell me :sob:
Where is my streamserver

I genuinely cant manage to read this anytype magic
Where is this “xev” defined?

It’s defined for you from one of these based on your OS. See if you can figure out how to set up a TCP client/server from this benchmark. I still couldn’t quite do that, yet :sweat_smile:

Oh sorry, the way I phrased that was missleading. I am doing this for a school project so I dont wanna use any libs except std. (Also to keep it relatively simple)

I was asking if you (or someone else for that matter) knows how to make a server with std.net master.
(Ideally but obv optionally with code samples)

The tests for std.net are really good sample code. That link goes straight to the test for a simple TCP serving scenario.

3 Likes

OHHH THATS GENIUSS the made it a method of address I LOVE IT thank you so much

1 Like

If you want to have a single threaded server, you can use std.io.poll:
zig/lib/std/io.zig at 5c0766b6c8f1aea18815206e0698953a35384a21 · ziglang/zig · GitHub.

For an example see zig/lib/std/child_process.zig at 5c0766b6c8f1aea18815206e0698953a35384a21 · ziglang/zig · GitHub.

An alternative is to use directly epoll, poll or select, depending on the OS you are using.

1 Like

Thank you, I will take a look at this!

epoll based example
When client connects, this function is invoked by the engine.

I am not sure that usage of std.io.poll is good idea for sockets

According to introduce std.io.poll it was intended for pipes

Let’s discuss