accept
fn accept(Stream listener) -> Result<Stream, IoError> Import
Source: use io::net::tcp::{accept}; compiler/src/builtin_docs/io/net/tcp.hy Accepts one pending connection on listener without waiting.
Returns the connected stream, or Err(IoError::WouldBlock) when no
connection is waiting; then call wait_readable(listener) and retry,
or use io::sync::accept_wait, which does both. Returns
Err(IoError::InvalidInput) when listener is not a stream from
listen. Needs no permission flag (the grant is checked at listen).
Parameters#
| Name | Type |
|---|---|
listener | Stream |
Returns#
Result<Stream, IoError>
Examples#
use io::{wait_readable, IoError};
use io::net::tcp::{accept, connect, listen, local_addr};
use tests::assert;
fn run() -> Result<bool, IoError> {
let listener = listen("127.0.0.1", 0)?;
let empty = match accept(listener) {
Result::Err(IoError::WouldBlock) => true,
default => false,
};
let (_, port) = local_addr(listener)?;
let _client = connect("127.0.0.1", port)?;
wait_readable(listener)?;
let _server = accept(listener)?;
return empty;
}
fn main() {
let ok = match run() {
Result::Ok(b) => b,
Result::Err(_) => false,
};
assert!(ok);
}
See also#
io::sync::accept_waitio::net::tcp::listenconnect: Opens a TCP connection tohostonport.connect_timeout: Opens a TCP connection tohostonport, giving up aftermsmilliseconds.local_addr: Returns the local(host, port)of a TCP listener or connected TCP stream.peer_addr: Returns the remote(host, port)of a connected TCP stream.set_nodelay: TurnsTCP_NODELAYon or off for a TCP stream.shutdown: Shuts down the reading half, the writing half, or both halves of a TCP stream.