pub struct TcpStream { /* private fields */ }Expand description
A goroutine-aware TCP stream socket.
Blocking reads and writes park the calling goroutine (via the netpoll backend) when the operation would block, resuming it when data is available or the send buffer has space.
TcpStream implements std::io::Read and std::io::Write (for both
&mut TcpStream and &TcpStream), so it works with any Rust I/O adapter
without unsafe wrapper code. Use try_clone to
split a connection into independent read and write halves.
Implementations§
Source§impl TcpStream
impl TcpStream
Sourcepub fn connect<A: ToSocketAddrs>(addr: A) -> Result<Self>
pub fn connect<A: ToSocketAddrs>(addr: A) -> Result<Self>
Connect to addr.
Parks the goroutine until the connection completes if it does not
complete immediately (which is typical for non-blocking connect).
Equivalent to net.Dial("tcp", addr) in Go.
Sourcepub fn read(&mut self, buf: &mut [u8]) -> Result<usize>
pub fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Read bytes from the stream into buf.
Parks the goroutine if no data is immediately available.
Sourcepub fn write(&mut self, buf: &[u8]) -> Result<usize>
pub fn write(&mut self, buf: &[u8]) -> Result<usize>
Write buf to the stream.
Parks the goroutine if the send buffer is full. Returns the number of
bytes written (may be less than buf.len()).
Sourcepub fn try_clone(&self) -> Result<TcpStream>
pub fn try_clone(&self) -> Result<TcpStream>
Duplicate this stream, creating a second TcpStream that refers to the
same underlying TCP connection.
The duplicate is an independent TcpStream with its own fd (via
dup(2)). Both streams share the same socket; reads and writes on
either half see the same data stream. Closing one does not close the
other.
The typical use-case is splitting a connection into a dedicated read half and a dedicated write half for use in separate goroutines.
Sourcepub fn peer_addr(&self) -> Result<SocketAddr>
pub fn peer_addr(&self) -> Result<SocketAddr>
Return the remote address of the peer this stream is connected to.
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Return the local address this stream is bound to.
Trait Implementations§
Source§impl Read for &TcpStream
Implements std::io::Read on a shared reference by issuing a raw
libc::read call. The fd is non-blocking; EAGAIN causes the goroutine
to park via netpoll exactly as the owned-&mut self path does.
impl Read for &TcpStream
Implements std::io::Read on a shared reference by issuing a raw
libc::read call. The fd is non-blocking; EAGAIN causes the goroutine
to park via netpoll exactly as the owned-&mut self path does.
This enables using the same TcpStream for both reading and writing from
two separate code sites within the same goroutine (e.g. after splitting
into read/write halves conceptually without calling try_clone).
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)cursor. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read. Read more1.0.0 · Source§fn chain<R>(self, next: R) -> Chain<Self, R>
fn chain<R>(self, next: R) -> Chain<Self, R>
Source§impl Read for TcpStream
Implements std::io::Read by delegating to TcpStream::read.
impl Read for TcpStream
Implements std::io::Read by delegating to TcpStream::read.
This allows TcpStream to be used with any Rust I/O adapter that accepts
impl Read, such as BufReader, Read::read_to_string, etc., without
any unsafe wrapper or raw-fd manipulation.
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)cursor. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read. Read more1.0.0 · Source§fn chain<R>(self, next: R) -> Chain<Self, R>
fn chain<R>(self, next: R) -> Chain<Self, R>
Source§impl Write for &TcpStream
Implements std::io::Write on a shared reference.
impl Write for &TcpStream
Implements std::io::Write on a shared reference.
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)Source§impl Write for TcpStream
Implements std::io::Write by delegating to TcpStream::write.
flush is a no-op because the kernel TCP stack handles buffering.
impl Write for TcpStream
Implements std::io::Write by delegating to TcpStream::write.
flush is a no-op because the kernel TCP stack handles buffering.
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)