local_addr
fn local_addr(Stream s) -> Result<(string, int), IoError> Import
Source: use io::net::tcp::{local_addr}; compiler/src/builtin_docs/io/net/tcp.hy Returns the local (host, port) of a TCP listener or connected TCP stream.
The host is an IP address; IPv6 addresses come in brackets
("[::1]"). Useful after listen(host, 0) to learn the port the OS
picked. Returns Err(IoError::InvalidInput) for a stream that is not
TCP and Err(IoError::AlreadyClosed) once it is closed.
Parameters#
| Name | Type |
|---|---|
s | Stream |
Returns#
Result<(string, int), IoError>
Examples#
use io::net::tcp::{listen, local_addr};
use tests::assert;
let listener = match listen("127.0.0.1", 0) {
Result::Ok(s) => s,
Result::Err(_) => panic "listen failed",
};
let (host, port) = match local_addr(listener) {
Result::Ok(addr) => addr,
Result::Err(_) => ("", 0),
};
assert!(host == "127.0.0.1" && port > 0);
See also#
accept: Accepts one pending connection onlistenerwithout waiting.connect: Opens a TCP connection tohostonport.connect_timeout: Opens a TCP connection tohostonport, giving up aftermsmilliseconds.listen: Opens a TCP socket listening onhostandport.peer_addr: Returns the remote(host, port)of a connected TCP stream.set_nodelay: TurnsTCP_NODELAYon or off for a TCP stream.shutdown: Shuts down the reading half, the writing half, or both halves of a TCP stream.