#error.Unexpected: GetLastError(59): An unexpected network error occurred.

1 messages · Page 1 of 1 (latest)

acoustic cosmos
#

I am attempting to make a simple Online-Terminal Messenger to dip my toes into networking. I am getting this error when I try to call "read" on a reader from a stream of a Server. Ignoring some simple helper functions (That I will Attach later) My code is this :

const std = @import("std");
const net = std.net;

pub fn main() !void {
    print("Test 1\n");

    const stdin = std.io.getStdIn().reader();

    const receiving_on = try net.Address.parseIp("127.0.0.1", 2335);

    printf("You are receiving on : {any}\n", .{receiving_on});

    print("Please Provide an IP Address :\n");
    var ip_buf : [20] u8 = undefined;
    const ip = try input_to_buf(stdin, &ip_buf) orelse return;

    print("Please Provide a Port\n");
    var port_buf : [10] u8 = undefined;
    const port_str = try input_to_buf(stdin, &port_buf) orelse return;
    const port = try std.fmt.parseInt(u16, port_str, 10);



    const address = try net.Address.parseIp(ip, port);



    _ = address;

    var reception_desk = try receiving_on.listen(.{});
    defer reception_desk.deinit();
    var reception_inbox = reception_desk.stream.reader();

    var inp_buf : [252]u8 = undefined;
    var msg_buf : [250]u8 = undefined;
    var msg_active : usize = 0;

    while (true) {
        print("What is your input? -r-eceive? or -s-end?\n");
        const msg = try input_to_buf(stdin, &inp_buf) orelse continue;

        if (str_eql(msg, "off", false)) break;

        if ( str_eql(msg, "r", false) ) {
            print("Attempting to receive\n");
            msg_active = try reception_inbox.read(&msg_buf);

            printf("Msg Received : -{s}-\n", .{msg_buf[0..msg_active]});
            
        } else if ( str_eql(msg, "s", false) ) {
            print("Attempting to send\n");
        }
    }
}
#

I have been trying to make this for a while now. I have had no luck, even when following a "TCP Server Tutorial" (https://www.openmymind.net/TCP-Server-In-Zig-Part-1-Single-Threaded/), However when I followed that (at step 5a) his working code, (copied and pasted), when I would run it, would never receive data. I don't know why. It would run without error, but it would never register that input was sent to it. ( I used his "Dummy Client" From Part 4 ).

Anyway here are my "helper functions"/function wrappers

const printf = std.debug.print;
inline fn print(str : []const u8) void {
    printf(str, .{});
}

pub fn input_to_buf(stdin : anytype, buf : []u8) !?[]const u8 {
    const inp_raw = try stdin.readUntilDelimiterOrEof(buf, '\n') orelse return null;
    return std.mem.trimRight(u8, inp_raw, "\r\n");
}

pub fn str_eql(str1 : []const u8, str2 : []const u8, caseLock : bool) bool {

    if (str1.len != str2.len) return false;

    if (caseLock == false) {

        for (str1, str2) |c1, c2| {
            if (c1 == c2) continue;

            const is_lowercase = (c1 >= 'a' and  c1 <= 'z');
            const is_capital = (c1 >= 'A' and  c1 <= 'Z');

            if (is_lowercase) {
                const offset = c1 - 'a';
                const new_char = offset + 'A';

                if (new_char != c2) return false;
            } else if (is_capital) {
                const offset = c1 - 'A';
                const new_char = offset + 'a';

                if (new_char != c2) return false;
            } else return false;
        }
    } else {
        for (str1, str2) |c1, c2| {
            if (c1 == c2) continue;
            return false;
        }
    }

    return true;
}```
#

The last line I see is "Attempting to receive" so that means the error must have occured here :

msg_active = try reception_inbox.read(&msg_buf);
``` If there was any doubt about where the error is located.
lament pasture
#

it's hard to understand the reason of the failure using snippets
Could you please publish your repo? I'll try to get a look later

acoustic cosmos
#

It is one file. 4 Functions, 1 While loop.

lament pasture
#

you need to call accept, because listening socket is not used for IO
/// Blocks until a client connects to the server. The returned Connection has
/// an open stream.
pub fn accept(s: *Server) AcceptError!Connection {
............

lament pasture
#

good news - you added accept in your new code 👍