Function in io::net::tcp compiler

local_addr

fn local_addr(Stream s) -> Result<(string, int), IoError>
Import
use io::net::tcp::{local_addr};
Source: 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#

NameType
sStream

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 on listener without waiting.
  • connect: Opens a TCP connection to host on port.
  • connect_timeout: Opens a TCP connection to host on port, giving up after ms milliseconds.
  • listen: Opens a TCP socket listening on host and port.
  • peer_addr: Returns the remote (host, port) of a connected TCP stream.
  • set_nodelay: Turns TCP_NODELAY on or off for a TCP stream.
  • shutdown: Shuts down the reading half, the writing half, or both halves of a TCP stream.
↑ ↓ navigate ↵ open