socket_addr
import std::net::addr::socket_addr; · source
Types
SocketAddr
type struct SocketAddr {
ip: IpAddr;
port: u16;
static new(ip: IpAddr, port: u16) -> SocketAddr;
static any_v4(port: u16) -> SocketAddr;
static localhost_v4(port: u16) -> SocketAddr;
static any_v6(port: u16) -> SocketAddr;
static localhost_v6(port: u16) -> SocketAddr;
static parse_v4(source: Str) -> Option<SocketAddr>;
}
An IP address paired with a port. This is what every socket constructor takes — TcpListener::bind, TcpConnect::start, UdpSocket::bind — and what local_addr() 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_v4 reads a.b.c.d:port, returning None if the address or port is malformed or the port is out of range.
import std::net::addr::socket_addr;
const addr: SocketAddr = SocketAddr::any_v4(8080);
POD — copied by value, nothing to drop.
Properties
| Property | Type |
|---|---|
ip | IpAddr |
port | u16 |
Constructors
static new(ip: IpAddr, port: u16) -> SocketAddr;
static any_v4(port: u16) -> SocketAddr;
static any_v4(port: u16) -> SocketAddr;0.0.0.0:port: bind on every IPv4 interface.
static localhost_v4(port: u16) -> SocketAddr;
static localhost_v4(port: u16) -> SocketAddr;127.0.0.1:port: bind/connect on the loopback interface.
static any_v6(port: u16) -> SocketAddr;
static any_v6(port: u16) -> SocketAddr;[::]:port: bind on every IPv6 interface.
static localhost_v6(port: u16) -> SocketAddr;
static localhost_v6(port: u16) -> SocketAddr;[::1]:port: bind/connect on the IPv6 loopback interface.
static parse_v4(source: Str) -> Option<SocketAddr>;
static parse_v4(source: Str) -> Option<SocketAddr>;Parse a.b.c.d:port. Returns None if the address or port is malformed or the port is out of range (u16).
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 SocketAddr {
ip: IpAddr;
port: u16;
}SocketAddr: (IP, port).
SocketAddr.ip: IpAddrtype enum IpAddr {
V4(IpV4Addr);
V6(IpV6Addr);
}SocketAddr.port: u16static 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 SocketAddr::new(ip: IpAddr, port: u16) -> SocketAddr(parameter) ip: IpAddr(parameter) port: u16static SocketAddr::any_v4(port: u16) -> SocketAddr0.0.0.0:port: bind on every IPv4 interface.
static SocketAddr::localhost_v4(port: u16) -> SocketAddr127.0.0.1:port: bind/connect on the loopback interface.
static SocketAddr::any_v6(port: u16) -> SocketAddr[::]:port: bind on every IPv6 interface.
static SocketAddr::localhost_v6(port: u16) -> SocketAddr[::1]:port: bind/connect on the IPv6 loopback interface.
static SocketAddr::parse_v4(source: Str) -> Option<SocketAddr>Parse a.b.c.d:port. Returns None if the address or port is malformed or the port is out of range (u16).
(parameter) source: Strtype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strimport 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).
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 addr: SocketAddrstatic SocketAddr::any_v4(port: u16) -> SocketAddr0.0.0.0:port: bind on every IPv4 interface.
type struct TcpListener {
fd: i32;
}static TcpListener::bind(local: SocketAddr) -> 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 TcpConnect {
fd: i32;
addr: [u8;
28];
addrlen: u32;
err: IoError;
rt: Reactor*;
}Connect to a remote address without blocking. The socket is opened and the handshake started when the future is created; each poll asks the OS whether it finished, parking on writability in between. The socket is closed if the future is dropped before it completes.
std::net::socket::tcpstatic TcpConnect::start(remote: SocketAddr) -> TcpConnectOpen a non-blocking socket and start connecting to remote.
type struct UdpSocket {
fd: i32;
}static UdpSocket::bind(local: SocketAddr) -> Result<UdpSocket, IoError>Open a datagram socket and bind it to local. Use SocketAddr::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_addr.
SocketAddr.port: u16static SocketAddr::localhost_v4(port: u16) -> SocketAddr127.0.0.1:port: bind/connect on the loopback interface.
static SocketAddr::parse_v4(source: Str) -> Option<SocketAddr>Parse a.b.c.d:port. Returns None if the address or port is malformed or the port is out of range (u16).
Option::NoneSocketAddr.ip: IpAddrstatic SocketAddr::new(ip: IpAddr, port: u16) -> SocketAddrstatic SocketAddr::any_v6(port: u16) -> SocketAddr[::]:port: bind on every IPv6 interface.
static SocketAddr::localhost_v6(port: u16) -> SocketAddr[::1]:port: bind/connect on the IPv6 loopback interface.