pub struct TcpStream { /* private fields */ }
Expand description
A connected TCP socket.
The potentially blocking methods (e.g., TcpStream::read
, TcpStream::write
) are meant
to be used in async contexts.
Examples
async fn echo_server(stream: TcpStream) -> Result {
let mut buf = [0u8; 1024];
loop {
let n = stream.read(&mut buf).await?;
if n == 0 {
return Ok(());
}
stream.write_all(&buf[..n]).await?;
}
}
Implementations
sourceimpl TcpStream
impl TcpStream
sourcepub fn read<'a>(
&'a self,
buf: &'a mut [u8]
) -> impl Future<Output = Result<usize>> + 'a
pub fn read<'a>(
&'a self,
buf: &'a mut [u8]
) -> impl Future<Output = Result<usize>> + 'a
Reads data from a connected socket.
Returns a future that when ready indicates the result of the read operation; on success, it contains the number of bytes read, which will be zero if the connection is closed.
Methods from Deref<Target = TcpStream>
sourcepub fn read(&self, buf: &mut [u8], block: bool) -> Result<usize>
pub fn read(&self, buf: &mut [u8], block: bool) -> Result<usize>
Reads data from a connected socket.
On success, returns the number of bytes read, which will be zero if the connection is closed.
If no data is immediately available for reading, one of two behaviours will occur:
- If
block
isfalse
, returnscrate::error::code::EAGAIN
; - If
block
istrue
, blocks until an error occurs, the connection is closed, or some becomes readable.
sourcepub fn write(&self, buf: &[u8], block: bool) -> Result<usize>
pub fn write(&self, buf: &[u8], block: bool) -> Result<usize>
Writes data to the connected socket.
On success, returns the number of bytes written.
If the send buffer of the socket is full, one of two behaviours will occur:
- If
block
isfalse
, returnscrate::error::code::EAGAIN
; - If
block
istrue
, blocks until an error occurs or some data is written.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for TcpStream
impl Send for TcpStream
impl Sync for TcpStream
impl Unpin for TcpStream
impl UnwindSafe for TcpStream
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more