#Close TCP server while accepting connections

1 messages · Page 1 of 1 (latest)

dull kestrel
#

Hey everyone. What's the correct way to accept multiple TCP connections with a std.net.Server and then close it from another thread?
I assumed calling deinit() on the server would unblock the accept() call and return an error, but it just keeps blocking.
Couldn't find any documentation on whether that's intentional and most tutorials either use the posix sockets directly or... just don't handle multiple connections at all.

Thanks in advance!

nimble knot
#

the short answer is that in general you don’t, the best you can do is set a flag that tells your code to not accept new connections and then open a connection to yourself so that you can kill the server

#

on certain platforms (cough winsock) it is illegal to attempt to shutdown the socket while accepting connections

dull kestrel
#

That kinda makes sense, but is that really the standard procedure to run a tcp server?
I've rarely seen that implemented and I remember in Java, for instance, calling close() would end the blocking call with an exception one was supposed to catch.
Are there any complete examples for Zig?
Anyway, thank you!

loud cipher