Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

net.addr

import net

Everything here is re-exported by net, so import net is enough and the names are called as net.*. Importing net.addr on its own works too and reaches the same definitions.

Socket addresses: an IP address and a port. SocketAddrV4 and SocketAddrV6 each represent an address of their respective family, while SocketAddr is a small wrapper around either one for code that needs to accept and work with both interchangeably just like the same relationship Ip has to Ipv4/Ipv6.

An IPv6 address may also carry a zone/scope identifier (RFC 4007), used to disambiguate a link-local address between network interfaces, e.g. fe80::1%3.

TcpStream, UdpSocket, and DtlsSocket return these from local_address()/peer_address().

import net

var server = net.TcpStream()
server.bind('0.0.0.0:8080')

var local = server.local_address()
echo 'listening on ${local.ip()}, port ${local.port()}'

Classes

SocketAddrV4

class net.SocketAddrV4

An IPv4 address and port, e.g. 192.168.1.10:8080.

  • printable — has a @to_string(), so echo and print() show something useful

Constructor

net.SocketAddrV4(address, port)

Returns a new instance of a SocketAddrV4.

Parameters

  • address (Ipv4) — The IP address
  • port (number) — The port, 0-65535

Raises Error if address is not an {Ipv4}, or port is missing or outside 0-65535

SocketAddrV4.parse()

net.SocketAddrV4.parse(address) -> SocketAddrV4

Parses an address given as ip:port, e.g. '192.168.1.10:8080'.

Parameters

  • address (string) — The address to parse

Returns SocketAddrV4

Raises Error if address isn’t a well-formed IPv4 address followed by : and a port from 0 to 65535

SocketAddrV4.ip()

net.SocketAddrV4.ip() -> Ipv4

The IP address.

Returns Ipv4

SocketAddrV4.port()

net.SocketAddrV4.port() -> number

The port.

Returns number

SocketAddrV4.equals()

net.SocketAddrV4.equals(other) -> bool

Whether other refers to the same address and port as this one.

Parameters

  • other (SocketAddrV4) — The address to compare against

Returns bool

SocketAddrV4.to_string()

net.SocketAddrV4.to_string() -> string

This address as ip:port, e.g. '192.168.1.10:8080'.

Returns string

SocketAddrV6

class net.SocketAddrV6

An IPv6 address and port, e.g. [2001:db8::1]:8080, optionally carrying a zone/scope identifier (RFC 4007) that disambiguates a link-local address between network interfaces, e.g. [fe80::1%3]:8080.

  • printable — has a @to_string(), so echo and print() show something useful

Constructor

net.SocketAddrV6(address, port, scope_id)

Returns a new instance of a SocketAddrV6.

Parameters

  • address (Ipv6) — The IP address
  • port (number) — The port, 0-65535
  • scope_id (?number) — The zone/scope identifier, or 0 (the default) if this address isn’t scoped to a particular interface

Raises Error if address is not an {Ipv6}, port is missing or outside 0-65535, or scope_id is negative

SocketAddrV6.parse()

net.SocketAddrV6.parse(address) -> SocketAddrV6

Parses an address given as [ip]:port or, for a scoped address, [ip%scope_id]:port, e.g. '[::1]:8080' or '[fe80::1%3]:8080'.

Parameters

  • address (string) — The address to parse

Returns SocketAddrV6

Raises Error if address isn’t a well-formed bracketed IPv6 address followed by : and a port from 0 to 65535

SocketAddrV6.ip()

net.SocketAddrV6.ip() -> Ipv6

The IP address.

Returns Ipv6

SocketAddrV6.port()

net.SocketAddrV6.port() -> number

The port.

Returns number

SocketAddrV6.scope_id()

net.SocketAddrV6.scope_id() -> number

The zone/scope identifier, or 0 if this address isn’t scoped to a particular network interface.

Returns number

SocketAddrV6.equals()

net.SocketAddrV6.equals(other) -> bool

Whether other refers to the same address, port, and scope as this one.

Parameters

  • other (SocketAddrV6) — The address to compare against

Returns bool

SocketAddrV6.to_string()

net.SocketAddrV6.to_string() -> string

This address as [ip]:port, or [ip%scope_id]:port if it carries a non-zero scope id.

Returns string

SocketAddr

class net.SocketAddr

Either a SocketAddrV4 or a SocketAddrV6, for code that needs to accept and work with socket addresses of either family without committing to one upfront.

  • printable — has a @to_string(), so echo and print() show something useful

Constructor

net.SocketAddr(inner)

Wraps an already-constructed SocketAddrV4 or SocketAddrV6. Application code should generally prefer SocketAddr.parse(), SocketAddr.v4(), or SocketAddr.v6() over calling this directly.

Parameters

  • inner (SocketAddrV4|SocketAddrV6) — The address to wrap

SocketAddr.v4()

net.SocketAddr.v4(address, port) -> SocketAddr

Wraps an IPv4 address and port as a SocketAddr.

Parameters

  • address (Ipv4) — The IP address
  • port (number) — The port, 0-65535

Returns SocketAddr

SocketAddr.v6()

net.SocketAddr.v6(address, port, scope_id) -> SocketAddr

Wraps an IPv6 address and port as a SocketAddr.

Parameters

  • address (Ipv6) — The IP address
  • port (number) — The port, 0-65535
  • scope_id (?number) — The zone/scope identifier, or 0

Returns SocketAddr

SocketAddr.parse()

net.SocketAddr.parse(address) -> SocketAddr

Parses address as either an IPv4 or IPv6 socket address, choosing the family based on whether it starts with [.

Parameters

  • address (string) — The address to parse, e.g. '192.168.1.10:8080' or '[::1]:8080'

Returns SocketAddr

Raises Error if address is not a well-formed IPv4 or IPv6 socket address

SocketAddr.is_v4()

net.SocketAddr.is_v4() -> bool

Whether this is a SocketAddrV4.

Returns bool

SocketAddr.is_v6()

net.SocketAddr.is_v6() -> bool

Whether this is a SocketAddrV6.

Returns bool

SocketAddr.as_v4()

net.SocketAddr.as_v4() -> ?SocketAddrV4

Returns the wrapped SocketAddrV4, or nil if this is an IPv6 address.

Returns ?SocketAddrV4

SocketAddr.as_v6()

net.SocketAddr.as_v6() -> ?SocketAddrV6

Returns the wrapped SocketAddrV6, or nil if this is an IPv4 address.

Returns ?SocketAddrV6

SocketAddr.ip()

net.SocketAddr.ip() -> Ip

The IP address, wrapped as an Ip so it can be worked with without committing to a family.

Returns Ip

SocketAddr.port()

net.SocketAddr.port() -> number

The port.

Returns number

SocketAddr.equals()

net.SocketAddr.equals(other) -> bool

Whether other refers to the same address as this one. An IPv4 address and an IPv6 address are never equal, even if one is the IPv4-mapped form of the other.

Parameters

  • other (SocketAddr) — The address to compare against

Returns bool

SocketAddr.to_string()

net.SocketAddr.to_string() -> string

This address in its family’s standard notation.

Returns string


2026, Richard Ore and Zuri contributors