SultanC
  1. Home
  2. Examples
  3. Networking

Parsing and printing IPv4 addresses; malformed addresses are rejected

net/ip_addresses.sn
// Parsing and printing IPv4 addresses; malformed addresses are rejected.
import std.io.println;
import std.net.ip;

def main(): int {
    let home: string = "192.168.1.20";
    match parse_ipv4(&home) with
    | result.ok(address) -> { println(ipv4_to_text(&address)); }
    | result.err(_) -> { println("invalid"); };
    let wrong: string = "300.1.2.3";
    match parse_ipv4(&wrong) with
    | result.ok(_) -> { println("unexpected"); }
    | result.err(_) -> { println("rejected 300.1.2.3"); };
    return 0;
}

Run it

With the interpreter, without compiling:

./build/sultanc run examples/en/net/ip_addresses.sn

Or compile it to an executable:

./build/sultanc examples/en/net/ip_addresses.sn -o build/example && ./build/example

Source on GitHub   All examples