pub struct TcpStream { /* private fields */ }Expand description
One connected blocking TCP byte stream.
This type owns its socket. Dropping it releases the socket; close makes
that lifecycle transition explicit and idempotent. A clone obtained with
try_clone shares the underlying TCP connection, including shutdown state.
Implementations§
Source§impl TcpStream
impl TcpStream
Sourcepub fn connect(host: &str, port: u16) -> Result<Self>
pub fn connect(host: &str, port: u16) -> Result<Self>
Connect to one resolved host and TCP port.
§Errors
Returns an OA I/O error if name resolution or connection fails.
Sourcepub fn read(&mut self, bytes: &mut [u8]) -> Result<usize>
pub fn read(&mut self, bytes: &mut [u8]) -> Result<usize>
Read at most bytes.len() bytes. Zero means peer EOF or an empty buffer.
§Errors
Returns FailedPrecondition after close, or Io for a socket failure.
Sourcepub fn write(&mut self, bytes: &[u8]) -> Result<usize>
pub fn write(&mut self, bytes: &[u8]) -> Result<usize>
Write at most bytes.len() bytes.
§Errors
Returns FailedPrecondition after close, or Io for a socket failure.
Sourcepub fn write_all(&mut self, bytes: &[u8]) -> Result<()>
pub fn write_all(&mut self, bytes: &[u8]) -> Result<()>
Write every byte or report the socket failure.
§Errors
Returns FailedPrecondition after close, or Io for a socket failure.
Sourcepub fn set_io_timeout(&self, timeout: Duration) -> Result<()>
pub fn set_io_timeout(&self, timeout: Duration) -> Result<()>
Set both read and write timeouts. Zero restores blocking behavior.
If configuring the write timeout fails, the prior read timeout is restored before the error is returned.
§Errors
Returns FailedPrecondition after close, or Io for a socket failure.
Sourcepub fn try_clone(&self) -> Result<Self>
pub fn try_clone(&self) -> Result<Self>
Return a second handle to the same connection, useful for interrupting a
blocking read from another thread with shutdown.
§Errors
Returns FailedPrecondition after close, or Io for clone failure.
Sourcepub fn shutdown(&self) -> Result<()>
pub fn shutdown(&self) -> Result<()>
Interrupt reads and writes on this connection and its clones.
§Errors
Returns FailedPrecondition after close, or Io for shutdown failure.
Sourcepub const fn peer_addr(&self) -> SocketAddr
pub const fn peer_addr(&self) -> SocketAddr
Return the resolved peer address captured when the stream was created.
Sourcepub const fn remote_addr(&self) -> IpAddr
pub const fn remote_addr(&self) -> IpAddr
Return the resolved peer IP address.
Sourcepub const fn remote_port(&self) -> u16
pub const fn remote_port(&self) -> u16
Return the resolved peer port.