Skip to content

Instantly share code, notes, and snippets.

@SpikedPaladin
Created November 17, 2023 12:52
Show Gist options
  • Save SpikedPaladin/178884157aa2631f7b177b1f7da4f545 to your computer and use it in GitHub Desktop.
Save SpikedPaladin/178884157aa2631f7b177b1f7da4f545 to your computer and use it in GitHub Desktop.
Vala Example Server-Client
public void test_server() {
    var service = new SocketService();
    try {
        service.add_inet_port(1500, null);
        service.incoming.connect((conn) => {
            try {
                var dis = new DataInputStream(conn.input_stream);
                message(@"Message received: $(dis.read_line())");
            } catch (Error e) {
                warning(e.message);
            }
            
            return false;
        });
    } catch (Error e) {
        warning(e.message);
    }
}

public void test_client() {
    SocketConnection? connection = null;
    var client = new SocketClient();
    try {
        connection = client.connect_to_host("localhost", 1500);
        message("Connected!");
        var o_stream = connection.get_output_stream();
        o_stream.write("Hello server!".data);
    } catch (Error e) {
        message(e.message);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment