method TcpListener.bind
Binds and listens (SO_REUSEADDR is set). Port 0 picks a free port: read it back from the field local.
Example:
use std.net { SocketAddr, TcpListener }
fn main() -> () | ParseError io, net {
let l = TcpListener.bind(SocketAddr.parse("127.0.0.1:0")?)
match l {
TcpListener ok => {
print("bound")
_ = ok.close()
}
NetError _ => print("cannot bind")
}
}Output
bound
pub(api) static fn bind(addr: SocketAddr, backlog: i64 = 1024) -> TcpListener | NetError netSince. 1.0.0-beta
Parameters
| Name | Type |
|---|---|
addr | SocketAddr |
backlog | i64 = 1024 |
Returns
Errors
NetError- `Unavailable` (address in use, no resources), `AccessDenied` (privileged port), `ProtocolError`.
Effects
net
Defined in: std/net/net.oxl:378