You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
`
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
The text was updated successfully, but these errors were encountered:
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.
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();
`
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
The text was updated successfully, but these errors were encountered: