connect
fn connect(string host, int port) -> Result<Stream, IoError> Import
Source: use io::net::udp::{connect}; compiler/src/builtin_docs/io/net/udp.hy Opens a UDP socket whose default peer is host on port.
The socket is bound to an ephemeral IPv4 port (0.0.0.0:0), so the
peer must be an IPv4 address. Afterwards io::write sends a datagram
to that peer and io::read receives one from it. Nothing is sent by
connect itself.
A program that reaches connect compiles only with --allow-net.
Parameters#
| Name | Type |
|---|---|
host | string |
port | int |
Returns#
Result<Stream, IoError>
Examples#
use io::{write, IoError};
use io::net::udp::{bind, connect, local_port};
use io::sync::{recv_from_wait};
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 = connect("127.0.0.1", port)?;
write(client, to_bytes("ping"))?;
let (n, _, _) = recv_from_wait(server, to_bytes("........"))?;
return n;
}
fn main() {
let n = match run() {
Result::Ok(n) => n,
Result::Err(_) => 0,
};
assert!(n == 4);
}