socket_address
import std::net::address::socket_address; · source
Types
SocketAddress
type struct SocketAddress {
ip: IpAddress;
port: u16;
}
An IP address paired with a port. This is what every socket constructor takes — TcpListener::bind, TcpStream::connect, UdpSocket::bind — and what local_address() hands back.
any_v4(port) is 0.0.0.0:port, binding on every IPv4 interface; localhost_v4(port) is 127.0.0.1:port; the _v6 pair are [::] and [::1]. parse reads an endpoint in the form Display writes: a.b.c.d:port, or an IPv6 address in brackets with the port after them, [::1]:8080 (RFC 3986). The port is a decimal number from 0 to 65535. It refuses with a ConversionError saying what was wrong when the text has neither form, its address does not parse, an IPv6 address is not in brackets, or the port is not such a number.
import std::net::address::socket_address::{ SocketAddress };
const address: SocketAddress = SocketAddress::any_v4(8080);
POD — copied by value, nothing to drop. Display prints a.b.c.d:port, or [v6]:port for an IPv6 address.
Properties
| Property | Type |
|---|---|
ip | IpAddress |
port | u16 |
Constructors
static new(ip: IpAddress, port: u16) -> SocketAddress;
static new(ip: IpAddress, port: u16) -> SocketAddress;The endpoint ip:port.
static any_v4(port: u16) -> SocketAddress;
static any_v4(port: u16) -> SocketAddress;0.0.0.0:port: bind on every IPv4 interface.
static localhost_v4(port: u16) -> SocketAddress;
static localhost_v4(port: u16) -> SocketAddress;127.0.0.1:port: bind/connect on the loopback interface.
static any_v6(port: u16) -> SocketAddress;
static any_v6(port: u16) -> SocketAddress;[::]:port: bind on every IPv6 interface.
static localhost_v6(port: u16) -> SocketAddress;
static localhost_v6(port: u16) -> SocketAddress;[::1]:port: bind/connect on the IPv6 loopback interface.
static parse(source: Str) -> Result<SocketAddress, ConversionError>;
static parse(source: Str) -> Result<SocketAddress, ConversionError>;Parse an endpoint in the form Display writes: a.b.c.d:port, or an IPv6 address in brackets with the port after them, [::1]:8080 (RFC 3986). The port is a decimal number from 0 to 65535. Refused, with what was wrong, when the text has neither form, its address does not parse, an IPv6 address is not in brackets, or the port is not such a number.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct SocketAddress {
ip: IpAddress;
port: u16;
}An IP address and a port: the endpoint a socket binds or connects to. Printed as a.b.c.d:port, or [v6]:port for IPv6.
SocketAddress.ip: IpAddresstype enum IpAddress {
V4(IpV4Address);
V6(IpV6Address);
}An IP address of either family. parse picks the family from the text (a : means IPv6), and Display writes the family's own form.
SocketAddress.port: u16import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
type struct SocketAddress {
ip: IpAddress;
port: u16;
}An IP address and a port: the endpoint a socket binds or connects to. Printed as a.b.c.d:port, or [v6]:port for IPv6.
const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const address: SocketAddressstatic SocketAddress::any_v4(port: u16) -> SocketAddress0.0.0.0:port: bind on every IPv4 interface.
type struct TcpListener {}A TCP socket bound to a local address and listening for connections. bind makes one, non-blocking; accept takes the next connection from it. Owns its socket and closes it on drop.
static TcpListener::bind(local: SocketAddress) -> Result<TcpListener, IoError>Bind + listen on local (IPv4 or IPv6). Sets SO_REUSEADDR so restarts don't wait out the TIME_WAIT window. Backlog defaults to 128, the same number Linux uses for /proc/sys/net/core/somaxconn these days.
type struct TcpStream {}A connected TCP socket. Comes from TcpStream::connect (client) or listener.accept() (server), both of which leave it in non-blocking mode; bytes move through read_some and write_all, or through BufferedStream<TcpStream> via its AsyncTransport implementation. Owns its socket and closes it on drop.
type struct UdpSocket {}An IPv4 datagram socket. bind or unbound opens one in non-blocking mode; datagrams move through send_to/receive_from, or send/receive once connect has pinned a peer. Owns its socket and closes it on drop.
static UdpSocket::bind(local: SocketAddress) -> Result<UdpSocket, IoError>Open a datagram socket and bind it to local. Use SocketAddress::any_v4(port) to receive on every interface or localhost_v4(port) for loopback. Pass port 0 to let the kernel pick an ephemeral port, then read it back with local_address.
SocketAddress.port: u16static SocketAddress::localhost_v4(port: u16) -> SocketAddress127.0.0.1:port: bind/connect on the loopback interface.
static SocketAddress::parse(source: Str) -> Result<SocketAddress, ConversionError>Parse an endpoint in the form Display writes: a.b.c.d:port, or an IPv6 address in brackets with the port after them, [::1]:8080 (RFC 3986). The port is a decimal number from 0 to 65535.
type trait Display {
fmt(&this, f: mut &Formatter) -> Result<(), FormatError>;
to_string(&this) -> String;
}How a value reads to a person: what {x} in an f-string and println write. Implement it for a type a user sees.
type struct ConversionError {}What went wrong in a TryFrom / TryInto. Carries a short static message describing the failure; richer diagnostics are a caller concern.
SocketAddress.ip: IpAddressstatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static SocketAddress::new(ip: IpAddress, port: u16) -> SocketAddressThe endpoint ip:port.
(parameter) ip: IpAddress(parameter) port: u16static SocketAddress::any_v6(port: u16) -> SocketAddress[::]:port: bind on every IPv6 interface.
static SocketAddress::localhost_v6(port: u16) -> SocketAddress[::1]:port: bind/connect on the IPv6 loopback interface.
(parameter) source: Strtype struct Str {}A read-only view of UTF-8 text owned elsewhere: a string literal, a String, a file's bytes. Copying a Str copies the view, not the text, and it is valid only while what it views is. Str::new("text") views a literal; String.as_str() views a String.
type enum Result<T, E> with [Eq, Clone, Debug] {
Ok(T);
Err(E);
}The outcome of an operation that can fail: Ok(value) on success, Err(error) on failure.