Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

receiveFrom() error.WouldBlock #60

Closed
G3UKB opened this issue Mar 29, 2023 · 1 comment
Closed

receiveFrom() error.WouldBlock #60

G3UKB opened this issue Mar 29, 2023 · 1 comment

Comments

@G3UKB
Copy link

G3UKB commented Mar 29, 2023

Hi, part way through first Zig project. I have some hardware that requires a broadcast. This is the code to send the request. I know the hardware is working as I have other implementations to test with.
`
const port: u32 = 1024;
try net.init();
defer net.deinit();

// Broadcast addr
const bcAddr = net.EndPoint{
    .address = net.Address{ .ipv4 = net.Address.IPv4.broadcast },
    .port = port,
};
// Bind addr
const bindAddr = net.EndPoint{
    .address = net.Address{ .ipv4 = net.Address.IPv4.any },
    .port = 10000,
};
// Create socket to broadcast to end point
var sock = try net.Socket.create(.ipv4, .udp);
try sock.setBroadcast(true);
try sock.setReadTimeout(1000000);
try sock.bind(bindAddr);
// Format discover packet
var data = std.mem.zeroes([MAX_MSG]u8);
data[0] = 0xEF;
data[1] = 0xFE;
data[2] = 0x02;
std.debug.print("Data: {any}\n", .{data});
// Send discover packet
var e = try sock.sendTo(bcAddr, data[0..MAX_MSG]);
std.debug.print("Send to resp: {}\n", .{e});
// Read response
try read_response(sock);

`
This works and says I sent 63 bytes. This is the read response code.

`
fn read_response(sock: net.Socket) !void {
var response: net.Socket.ReceiveFrom = undefined;
var count: u32 = 5;
var data = std.mem.zeroes([MAX_MSG]u8);
std.time.sleep(1000000000);
while (true) {
response = sock.receiveFrom(&data) catch |err| {
std.debug.print("Discover fail, retrying: {}\n", .{err});
const stderr = std.io.getStdErr();
_ = stderr;
count -= 1;
if (count <= 0) {
break;
} else {
std.time.sleep(1000000000);
continue;
}
};
}
std.debug.print("Discover resp: {any}, {any}\n", .{ response.numberOfBytes, response.sender });
std.debug.print("Discover data: {any}\n", .{data});
}

`
This just returns WouldBlock every attempt. The sleeps and retries make no difference. I read something about the zig event loop in another post so just tried 'async' which told me it wasn't implemented so I ran the whole discover on a thread but still made no difference, Not sure where to go from here.
Bob

@G3UKB
Copy link
Author

G3UKB commented Mar 29, 2023

Turns out this was caused by the bind address of 0,0,0,0 binding to my VirtualBox address which of course was never receiving any data. Binding to the IP of my m/c and it works fine.

@G3UKB G3UKB closed this as completed Mar 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant