recv_from
fn recv_from(Stream s, Vec<byte> buf) -> Result<(int, string, int), IoError> Import
Source: use io::net::udp::{recv_from}; compiler/src/builtin_docs/io/net/udp.hy Receives one datagram into the front of buf without waiting.
Returns (n, host, port): the number of bytes written to buf[0..n]
and the sender’s IP address (IPv6 without brackets) and port. A
datagram longer than buf.len() is cut to fit; the rest is lost.
Returns Err(IoError::WouldBlock) when no datagram is waiting; then
call wait_readable(s) and retry, or use io::sync::recv_from_wait.
An empty buf returns (0, "", 0) at once.
Needs no permission flag (the grant is checked at bind).
Parameters#
| Name | Type |
|---|---|
s | Stream |
buf | Vec<byte> |
Returns#
Result<(int, string, int), IoError>
Examples#
use io::{wait_readable, IoError};
use io::net::udp::{bind, local_port, recv_from, send_to};
use string::{to_bytes};
use tests::assert;
fn run() -> Result<(int, string, int), IoError> {
let server = bind("127.0.0.1", 0)?;
let port = local_port(server)?;
let client = bind("127.0.0.1", 0)?;
send_to(client, to_bytes("hi"), "127.0.0.1", port)?;
wait_readable(server)?;
return recv_from(server, to_bytes("........"))?;
}
fn main() {
let (n, host, port) = match run() {
Result::Ok(t) => t,
Result::Err(_) => (0, "", 0),
};
assert!(n == 2 && host == "127.0.0.1" && port > 0);
}
See also#
io::sync::recv_from_waitio::net::udp::bindconnect: Opens a UDP socket whose default peer ishostonport.local_port: Returns the local port a UDP socket is bound to.send_to: Sendsbufas one datagram tohostonportand returns how many bytes were sent.