Boring issue regarding some posix sockets API.
Zig 0.14.1 (but the same will be also on 015.1).
std.posix.connect failes on already connected socket -
reached unreachable code
.ISCONN => unreachable, // The socket is already connected
Great! Code “knows” that socket is already conected, but instead of returning error.AlreadyConnected decision was to fail ![]()
It’s completely OK for Linux - looks that it does not return ISCONN for this case, but MacOS does and my code on mac failed.
One of the possible solution is to use getsockoptError(), but the same procesing of .ISCONN
.ISCONN => unreachable, // The socket is already connected.
Only setsockopt() returns AlreadyConnected for connected socket, for others - unreachable
so far not so good
I wrote simple function - send zero length buffer to the socket (btw I am working with non-blocking sockets):
pub fn knock(socket: std.posix.socket_t) bool {
log.debug("knock-knock", .{});
const slice: [1]u8 = .{0};
_ = sendBufTo(socket, slice[0..0]) catch |err| {
log.debug("knock error {s}", .{@errorName(err)});
return false;
};
return true;
}
where sendBufTo is my internal function, it uses ‘std.posix.sendto’. knock() should return true for connected socket and false otherwise.
It works ![]()
But may be you know simpler way?