data TcpStream
A connected TCP stream. @must_close: end it with close() (use defer).
Example:
use std.net { SocketAddr, TcpListener, TcpStream, IpAddr }
fn main() -> () | NetError | ParseError io, net {
let l = TcpListener.bind(SocketAddr.parse("127.0.0.1:0")?)?
defer l.close()?
let c = TcpStream.connect(l.local, 2s)?
defer c.close()?
let s = l.accept(3s)?
defer s.close()?
c.writeText("ping")?
print(s.readText(16)?)
}Вывод
ping
@must_close
pub(api) data TcpStreamПотокобезопасность. one task reads and one task writes at a time; do not share a stream between two readers.
С версии. 1.0.0-beta
Поля
pub(api) local: SocketAddr | Address of this end. |
pub(api) peer: SocketAddr | Address of the other end. |
Методы
connect | (addr: SocketAddr, timeout: Duration?) -> TcpStream | NetError netConnects to addr (TCP_NODELAY is set). |
connectHost | (host: str, port: u16, timeout: Duration?) -> TcpStream | NetError netResolves host (a name or numeric address) and connects to the first address that accepts. |
read | (max: usize, timeout: Duration?) -> bytes | NetError netUp to max bytes. |
readText | (max: usize, timeout: Duration?) -> str | NetError netLike read, for UTF-8 text; bytes that are not valid UTF-8 give ProtocolError. |
write | (buf: bytes, timeout: Duration?) -> () | NetError netWrites all of data. |
writeText | (text: str, timeout: Duration?) -> () | NetError netWrites the UTF-8 bytes of text. |
shutdownWrite | () -> () | NetError netCloses the sending side (the peer reads end of stream) and keeps receiving. |
setNoDelay | (on: bool) -> () | NetError netTurns TCP_NODELAY (no coalescing of small writes) on or off; it is on after connect and accept. |
close | () -> () | NetError netCloses the stream. |
Определено в: std/net/net.oxl:461