Most solutions on search return the std.os.socket
but os
package no longer has the socket method defined.
const std = @import("std");
pub fn main() !void {
const addr = try std.Address.parseIp("127.0.0.1", 8080);
const sock_fd = try std.os.socket(std.os.AF.INET, std.os.SOCK.DGRAM, std.os.IPPROTO.UDP);
defer std.os.close(sock_fd);
try std.os.bind(sock_fd, &addr.any, addr.getOsSockLen());
var buf: [1024]u8 = undefined;
const len = try std.os.recv(sock, &buf, 0);
std.debug.print("{s}\n", .{buf[0..len]});
}
Most solutions on search return the std.os.socket
but os
package no longer has the socket method defined.
const std = @import("std");
pub fn main() !void {
const addr = try std.net.Address.parseIp("127.0.0.1", 8080);
const sock_fd = try std.os.socket(std.os.AF.INET, std.os.SOCK.DGRAM, std.os.IPPROTO.UDP);
defer std.os.close(sock_fd);
try std.os.bind(sock_fd, &addr.any, addr.getOsSockLen());
var buf: [1024]u8 = undefined;
const len = try std.os.recv(sock, &buf, 0);
std.debug.print("{s}\n", .{buf[0..len]});
}
Share
Improve this question
edited 2 days ago
Cijin Cherian
asked Feb 6 at 1:47
Cijin CherianCijin Cherian
1501 silver badge9 bronze badges
1 Answer
Reset to default 0I found the answer on Ziggit, posting it here for better reach.
Thank you dimdim for the solution.
const std = @import("std");
pub fn main() !void {
const sock = try std.posix.socket(std.posix.AF.INET, std.posix.SOCK.DGRAM, std.posix.IPPROTO.UDP);
defer std.posix.close(sock);
const addr = try std.net.Address.parseIp("127.0.0.1", 55555);
try std.posix.bind(sock, &addr.any, addr.getOsSockLen());
var buf: [1024]u8 = undefined;
const len = try std.posix.recvfrom(sock, &buf, 0, null, null);
std.debug.print("recv: {s}\n", .{buf[0..len]});
}