Function in io::net::udp compiler

send_to

fn send_to(Stream s, Vec<byte> buf, string host, int port) -> Result<int, IoError>
Import
use io::net::udp::{send_to};
Source: 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#

NameType
sStream
bufVec<byte>
hoststring
portint

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#

↑ ↓ navigate ↵ open