send_to
fn send_to(Stream s, Vec<byte> buf, string host, int port) -> Result<int, IoError> Import
Source: use io::net::udp::{send_to}; compiler/src/builtin_docs/io/net/udp.hy Sends buf as one datagram to host on port and returns how many bytes were sent.
Does not wait: returns Err(IoError::WouldBlock) when the OS send
buffer is full (io::sync::send_to_wait waits and retries). host is
resolved like in bind. Returns Err(IoError::InvalidInput) for a port
outside 0..=65535.
A program that reaches send_to compiles only with --allow-net.
Parameters#
| Name | Type |
|---|---|
s | Stream |
buf | Vec<byte> |
host | string |
port | int |
Returns#
Result<int, IoError>
Examples#
use io::{IoError};
use io::net::udp::{bind, local_port, send_to};
use string::{to_bytes};
use tests::assert;
fn run() -> Result<int, IoError> {
let server = bind("127.0.0.1", 0)?;
let port = local_port(server)?;
let client = bind("127.0.0.1", 0)?;
return send_to(client, to_bytes("hello"), "127.0.0.1", port)?;
}
fn main() {
let sent = match run() {
Result::Ok(n) => n,
Result::Err(_) => 0,
};
assert!(sent == 5);
}
See also#
io::sync::send_to_waitio::net::udp::bindconnect: Opens a UDP socket whose default peer ishostonport.local_port: Returns the local port a UDP socket is bound to.recv_from: Receives one datagram into the front ofbufwithout waiting.