Function in io::net::udp compiler

bind

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

Opens a UDP socket bound to host and port.

port may be 0 to let the OS pick a free port; read it back with local_port. A host name is resolved and only the first address is used, so prefer an explicit IP such as "127.0.0.1" or "0.0.0.0". Exchange datagrams with send_to and recv_from.

Returns Err(IoError::InvalidInput) for a port outside 0..=65535. A program that reaches bind compiles only with --allow-net.

Parameters#

NameType
hoststring
portint

Returns#

Result<Stream, IoError>

Examples#

use io::{IoError};
use io::net::udp::{bind, local_port};
use tests::assert;
fn run() -> Result<int, IoError> {
    let sock = bind("127.0.0.1", 0)?;
    return local_port(sock)?;
}

fn main() {
    let port = match run() {
        Result::Ok(p) => p,
        Result::Err(_) => 0,
    };
    assert!(port > 0);
}

See also#

↑ ↓ navigate ↵ open