bind
fn bind(string host, int port) -> Result<Stream, IoError> Import
Source: use io::net::udp::{bind}; 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#
| Name | Type |
|---|---|
host | string |
port | int |
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#
io::net::udp::local_portio::net::udp::send_toio::net::udp::recv_fromconnect: Opens a UDP socket whose default peer ishostonport.