server, client
1. add server and client demo
This commit is contained in:
parent
ceaa6e3b1a
commit
0a26401108
35
build.zig
35
build.zig
|
@ -4,26 +4,29 @@ pub fn build(b: *std.Build) void {
|
|||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "nation",
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
const server = b.addExecutable(.{
|
||||
.name = "nations",
|
||||
.root_source_file = b.path("src/server.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
const client = b.addExecutable(.{
|
||||
.name = "nationc",
|
||||
.root_source_file = b.path("src/client.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
b.installArtifact(exe);
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
b.installArtifact(server);
|
||||
b.installArtifact(client);
|
||||
const server_run_cmd = b.addRunArtifact(server);
|
||||
const client_run_cmd = b.addRunArtifact(client);
|
||||
|
||||
run_cmd.step.dependOn(b.getInstallStep());
|
||||
server_run_cmd.step.dependOn(b.getInstallStep());
|
||||
client_run_cmd.step.dependOn(b.getInstallStep());
|
||||
|
||||
// command itself, like this: `zig build run -- arg1 arg2 etc`
|
||||
if (b.args) |args| {
|
||||
run_cmd.addArgs(args);
|
||||
}
|
||||
|
||||
// This creates a build step. It will be visible in the `zig build --help` menu,
|
||||
// and can be selected like this: `zig build run`
|
||||
// This will evaluate the `run` step rather than the default, which is "install".
|
||||
const run_step = b.step("run", "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
const server_run_step = b.step("runs", "Run the server");
|
||||
const client_run_step = b.step("runc", "Run the client");
|
||||
server_run_step.dependOn(&server_run_cmd.step);
|
||||
client_run_step.dependOn(&client_run_cmd.step);
|
||||
}
|
||||
|
|
24
src/client.zig
Normal file
24
src/client.zig
Normal file
|
@ -0,0 +1,24 @@
|
|||
const std = @import("std");
|
||||
const log = std.log;
|
||||
const net = std.net;
|
||||
const mem = std.mem;
|
||||
const Allocator = mem.Allocator;
|
||||
|
||||
pub fn main() !void {
|
||||
log.info("Hello, {s}", .{"client"});
|
||||
|
||||
const ip = "127.0.0.1";
|
||||
const port = 10010;
|
||||
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer {
|
||||
_ = gpa.deinit();
|
||||
}
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
const stream = try net.tcpConnectToHost(allocator, ip, port);
|
||||
_ = try stream.writeAll("hello");
|
||||
defer {
|
||||
stream.close();
|
||||
}
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
const std = @import("std");
|
||||
const log = std.log;
|
||||
|
||||
pub fn main() !void {
|
||||
log.info("Hello, {s}\n", .{"World!"});
|
||||
}
|
23
src/server.zig
Normal file
23
src/server.zig
Normal file
|
@ -0,0 +1,23 @@
|
|||
const std = @import("std");
|
||||
const log = std.log;
|
||||
const net = std.net;
|
||||
|
||||
pub fn main() !void {
|
||||
const ip = "127.0.0.1";
|
||||
const port: u16 = 10010;
|
||||
|
||||
// listen to local ip:port
|
||||
const address = try net.Address.parseIp(ip, port);
|
||||
log.info("server listen to {s}:{d}", .{ ip, port });
|
||||
// this listen function need two param, net.Address and options, so we need make a param address before there
|
||||
var server = try net.Address.listen(address, .{ .reuse_port = false });
|
||||
defer server.deinit();
|
||||
|
||||
while (true) {
|
||||
// accept from client
|
||||
const client = try server.accept();
|
||||
var buffer: [1024]u8 = undefined;
|
||||
const len = try client.stream.readAll(buffer[0..]);
|
||||
log.info("data from client: {s}\n", .{buffer[0..len]});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user