Function in io::net::udp compiler

local_port

fn local_port(Stream s) -> Result<int, IoError>
Import
use io::net::udp::{local_port};
Source: compiler/src/builtin_docs/io/net/udp.hy

Returns the local port a UDP socket is bound to.

Useful after bind(host, 0) or connect to learn the port the OS picked. Returns Err(IoError::AlreadyClosed) when s is not an open UDP socket.

Parameters#

NameType
sStream

Returns#

Result<int, IoError>

Examples#

use io::net::udp::{bind, local_port};
use tests::assert;
let sock = match bind("127.0.0.1", 0) {
    Result::Ok(s) => s,
    Result::Err(_) => panic "bind failed",
};
let port = match local_port(sock) {
    Result::Ok(p) => p,
    Result::Err(_) => 0,
};
assert!(port > 0 && port <= 65535);

See also#

  • io::net::udp::connect
  • bind: Opens a UDP socket bound to host and port.
  • recv_from: Receives one datagram into the front of buf without waiting.
  • send_to: Sends buf as one datagram to host on port and returns how many bytes were sent.
↑ ↓ navigate ↵ open