Last active
December 22, 2022 03:50
-
-
Save tetsu-koba/9afbab28bb12cb66e7359d13b043335b to your computer and use it in GitHub Desktop.
UDP connection example for Zig language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UDP connection example for Zig language |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const os = std.os; | |
const log = std.log; | |
const time = std.time; | |
const UDP_PAYLOADSIZE = 65507; | |
fn helloToServer(ipaddr: []const u8, port: u16, verbose:bool) !void { | |
var buf: [UDP_PAYLOADSIZE]u8 = .{}; | |
const sockfd = try os.socket(os.AF.INET, os.SOCK.DGRAM | os.SOCK.CLOEXEC, 0); | |
defer os.closeSocket(sockfd); | |
const addr = try std.net.Address.resolveIp(ipaddr, port); | |
try os.connect(sockfd, &addr.any, addr.getOsSockLen()); | |
const message = "Hello Server, this is zig client"; | |
const send_bytes = try os.send(sockfd, message, 0); | |
if (verbose) { | |
log.info("{d}: send_bytes={d}", .{time.milliTimestamp(), send_bytes}); | |
} | |
const recv_bytes = try os.recv(sockfd, buf[0..], 0); | |
if (verbose) { | |
log.info("{d}: recv_bytes={d}, [{s}]", .{time.milliTimestamp(), recv_bytes, buf[0..recv_bytes]}); | |
} | |
} | |
pub fn main() !void { | |
// TODO: get parameters from command line options | |
const verbose = true; | |
const port: u16 = 7200; | |
const adr = "127.0.0.1"; | |
try helloToServer(adr, port, verbose); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const os = std.os; | |
const log = std.log; | |
const time = std.time; | |
const UDP_PAYLOADSIZE = 65507; | |
fn serverLoop(port: u16) !void { | |
const ip4_addr = "0.0.0.0"; | |
var buf: [UDP_PAYLOADSIZE]u8 = .{}; | |
const sockfd = try os.socket(os.AF.INET, os.SOCK.DGRAM | os.SOCK.CLOEXEC, 0); | |
defer os.closeSocket(sockfd); | |
const addr = try std.net.Address.resolveIp(ip4_addr, port); | |
try os.bind(sockfd, &addr.any, addr.getOsSockLen()); | |
log.info("{d}: ready. port={d}", .{time.milliTimestamp(), port}); | |
var cliaddr: std.os.linux.sockaddr = undefined; | |
var cliaddrlen: std.os.socklen_t = @sizeOf(os.linux.sockaddr); | |
while (true) { | |
const recv_bytes = try os.recvfrom(sockfd, buf[0..], 0, &cliaddr, &cliaddrlen); | |
log.info("{d}: recv_bytes={d}, [{s}]", .{time.milliTimestamp(), recv_bytes, buf[0..recv_bytes]}); | |
const message = "Hi client, this is zig server."; | |
const send_bytes = try os.sendto(sockfd, message, 0, &cliaddr, cliaddrlen); | |
log.info("{d}: send_bytes={d}", .{time.milliTimestamp(), send_bytes}); | |
} | |
} | |
pub fn main() !void { | |
// TODO: get parameters from command line options | |
const port: u16 = 7200; | |
try serverLoop(port); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment