IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

SocketAddr

struct SocketAddr

Represents either an IPv4 or an IPv6 socket address.

Implemented traits​

AnyType, Copyable, Deinitable, EnumLike, Equatable, Hashable, ImplicitlyCopyable, Movable, Writable, __Enum

Methods​

__init__​

def __init__(out self, ip: IpAddr, port: UInt16)

Constructs a socket address in the family of ip.

An IPv6 socket address gets zero for its flow information and scope ID.

Args:

  • ​ip (IpAddr): The IP address.
  • ​port (UInt16): The port number in host byte order.

__eq__​

def __eq__(self, other: Self) -> Bool

Returns whether two socket addresses are equal.

Args:

  • ​other (Self): The socket address to compare against.

Returns:

Bool: True if both socket addresses have the same family and value.

ip​

def ip(self) -> IpAddr

Returns the IP address.

Returns:

IpAddr: The IP address.

port​

def port(self) -> UInt16

Returns the port number.

Returns:

UInt16: The port number in host byte order.

__hash__​

def __hash__(self, mut hasher: T)

Hashes the socket address using the given hasher.

Args:

  • ​hasher (T): The hasher instance.

write_to​

def write_to(self, mut writer: T)

Writes the socket address in the notation of its family.

Args:

  • ​writer (T): The writer to write to.

v4​

static def v4(var payload: SocketAddrV4, /) -> Self

An IPv4 socket address.

v6​

static def v6(var payload: SocketAddrV6, /) -> Self

An IPv6 socket address.