Help me I’m a
I’ve been working on this little server for my job.
Long and short of it is that it is a completion engine for some command line functions.
SO,
There is a tcp server I wrote out there chilin and the shell completion will interface with said server via tcp as a client.
BUT,
I can’t get the tcp read to work. I’ve no clue why.
I’ve tried various functions. read readall readbyte readdelimiter etc.
nada.
Just using an echo | nc
works though.
Here is the stack where it just hangs on the read call. Looks like I make it all the way to a syscall so it is probably just me not fully understanding tcp or something.
Sorry for trim. I don’t want to leak company info.
I have client → write → server read → server write → hang on client read.
Associated server code
while (true) {
print("Listening on {}\n", .{addr.getPort()});
var client = try server.accept();
print("Connection received! {} is sending data.\n", .{client.address});
const message = try client.stream.reader().readAllAlloc(gpa_allocator, 1024);
print("Recieved data.\n", .{});
defer gpa_allocator.free(message);
if (uris.search_uris(message)) {
print("found {s}\n", .{message});
print("about to write: \n", .{});
_ = try client.stream.writer().writeAll("hello\t");
client.stream.close();
print("post write: \n", .{});
} else {
print("failed to locate {s}.", .{message});
_ = try client.stream.writer().writeAll("no-op\t");
client.stream.close();
// uris.print();
}
}
Associated client code
// Connect to peer
const stream = try std.net.tcpConnectToAddress(peer);
defer stream.close();
// Sending data to peer
const data = "/features";
var writer = stream.writer();
_ = try writer.writeAll(data);
std.debug.print("post write", .{});
var resp_buffer: [1000]u8 = undefined;
// std.debug.print("about to read", .{});
// _ = try reader.readUntilDelimiter(&resp_buffer, '\t');
var reader = stream.reader();
const byte = try reader.read(&resp_buffer);
std.debug.print("byte: {}", .{byte});
// END OF MAIN
Apologies for weird naming. It’s the remains of me struggling to get it to work. It has to be something simple and small. <me