Skip to content
FutureBlitz

    Install

    Import std.net.lan

    std.net.lan

    std.net's LAN discovery: LanAnnouncer, which offers a service to the LAN, and Lan.Find, which finds those of a type there, over mDNS and DNS-SD (RFCs 6762 and 6763), as Bonjour and Avahi have them, so the dns-sd and avahi-browse tools find what a program offers, and it finds what they offer. It goes over IPv4, through each of the machine's interfaces but loopback's, or through loopback's when nothing reaches the LAN, so programs on the machine still find each other.

    std/net/lan.fb

    Types

    LanService struct

    Public Struct LanService Implements Printable

    A service on the LAN, as DNS-SD has one: its type, like "_mygame._udp", its name, which none of its type shares, the host it's on and that host's addresses, its port, and its text, a map of keys to values, each key once whatever its letters' case, as the first of them is written.

    std/net/lan.fb, line 62

    • Public ReadOnly Field Type: String field #

      Its type, like "_mygame._udp".

    • Public ReadOnly Field Name: String field #

      Its name, which none of its type shares.

    • Public ReadOnly Field Host: String field #

      The host it's on.

    • Public ReadOnly Field Port: UInt16 field #

      Its port.

    • Public ReadOnly Field Addresses: IpAddress[] field #

      Its host's addresses.

    • Public ReadOnly Field Text: Map<String, String> field #

      Its text: keys and their values.

    • Public Property Address: SocketAddress? property #

      Its first address and port, or Null when it has no address: the address its answer came from, which the finder can reach, then those it gave for its host.

    • Public Method ToString(): String method #

      Its text, as a string interpolation like "{value}" shows it.

      Doc from std.core.interfaces.Printable.ToString.

    LanAnnouncer class

    Public Class LanAnnouncer

    Offers a service to the LAN: it answers what Lan.Find and DNS-SD's other browsers ask, on the main thread, as Async code does, from Start until Stop, or until it goes, keeping the program running meanwhile, as a listener's AcceptAsync does. It doesn't check that no other service of its type has its name, as a DNS-SD responder's probe would, so names should differ, by the machine's name, say.

    std/net/lan.fb, line 100

    • @PanicsAtCaller Public Static Method Start(type: String, name: String, port: UInt16, text: Map<String, String>? = Null, discoveryPort: UInt16 = MDNS_PORT): LanAnnouncer Throws NetError static method #

      Starts offering the service name of type, which is "_" and up to 15 lowercase letters, digits, and '-', then "._udp" or "._tcp", at port of this machine, with text as its keys and values, each pair 255 bytes at most and 8192 in all, each key once whatever its letters' case. A type, name, or text that isn't one panics. It answers at mDNS's port, 5353, or at discoveryPort, which Find has to be given too. What it sends through an interface gives that interface's address alone, as RFC 6762 has it.

    • Public Property Service: LanService property #

      Its service, as Find gives it, with this machine's addresses.

    • Public Property IsStopped: Bool property #

      Whether Stop stopped it.

    • Public Method Stop() method #

      Says goodbye to the LAN, so other mDNS caches drop the service's records, but not its host's addresses, which other services can have, and stops answering.

    Lan struct

    Public Struct Lan

    Finds the services on the LAN.

    std/net/lan.fb, line 161

    • @PanicsAtCaller Public Static Method Find(type: String, timeout: Duration? = Null, discoveryPort: UInt16 = MDNS_PORT): LanService[] Throws NetError static method #

      The services of type, as LanAnnouncer.Start takes it, that answer within timeout, a second unless it's given, in the order they answered, each once. It asks again a third of the way through, as answers can be lost. Throws a NetError when it can't ask, as when the machine has no network. A type that isn't one panics.

    • @PanicsAtCaller Public Static Async Method FindAsync(type: String, timeout: Duration? = Null, discoveryPort: UInt16 = MDNS_PORT): LanService[] Throws NetError static method #

      Find's Async twin, which waits on the main thread.

    • Public Static Method LocalAddresses(): IpAddress[] static method #

      The addresses this machine has on its networks, but loopback's, IPv4's and IPv6's, as a LAN game shows its players where to join.

    • Public Static Method HostName(): String static method #

      This machine's name, as its OS has it.

    Report an issue with this page or with FutureBlitz on GitHub.