pub struct TcpStream { /* private fields */ }
Expand description
A non-blocking TCP stream between a local socket and a remote socket.
This works much like the TcpStream
in the standard library, but the
Read
and Write
implementation don’t block and instead return a
WouldBlock
error.
Deregistering
TcpStream
will deregister itself when dropped.
Examples
use std::time::Duration;
use mio_st::event::{Events, EventedId};
use mio_st::net::TcpStream;
use mio_st::poll::{Poller, PollOption};
let address = "127.0.0.1:8000".parse()?;
let mut stream = TcpStream::connect(address)?;
let mut poller = Poller::new()?;
let mut events = Events::new();
// Register the socket with `Poller`.
poller.register(&mut stream, EventedId(0), TcpStream::INTERESTS, PollOption::Edge)?;
poller.poll(&mut events, None)?;
// The socket might be ready at this point.
Implementations§
source§impl TcpStream
impl TcpStream
sourcepub const INTERESTS: Interests = Interests::BOTH
pub const INTERESTS: Interests = Interests::BOTH
The interests to use when registering to receive both readable and writable events.
sourcepub fn connect(address: SocketAddr) -> Result<TcpStream>
pub fn connect(address: SocketAddr) -> Result<TcpStream>
Create a new TCP stream and issue a non-blocking connect to the specified address.
sourcepub fn peer_addr(&mut self) -> Result<SocketAddr>
pub fn peer_addr(&mut self) -> Result<SocketAddr>
Returns the socket address of the remote peer of this TCP connection.
sourcepub fn local_addr(&mut self) -> Result<SocketAddr>
pub fn local_addr(&mut self) -> Result<SocketAddr>
Returns the socket address of the local half of this TCP connection.
sourcepub fn set_ttl(&mut self, ttl: u32) -> Result<()>
pub fn set_ttl(&mut self, ttl: u32) -> Result<()>
Sets the value for the IP_TTL
option on this socket.
sourcepub fn set_nodelay(&mut self, nodelay: bool) -> Result<()>
pub fn set_nodelay(&mut self, nodelay: bool) -> Result<()>
Sets the value of the TCP_NODELAY
option on this socket.
sourcepub fn nodelay(&mut self) -> Result<bool>
pub fn nodelay(&mut self) -> Result<bool>
Gets the value of the TCP_NODELAY
option on this socket.
sourcepub fn peek(&mut self, buf: &mut [u8]) -> Result<usize>
pub fn peek(&mut self, buf: &mut [u8]) -> Result<usize>
Receives data on the socket from the remote address to which it is connected, without removing that data from the queue. On success, returns the number of bytes peeked.
Successive calls return the same data. This is accomplished by passing
MSG_PEEK
as a flag to the underlying recv system call.
sourcepub fn shutdown(&mut self, how: Shutdown) -> Result<()>
pub fn shutdown(&mut self, how: Shutdown) -> Result<()>
Shuts down the read, write, or both halves of this connection.
This function will cause all pending and future I/O on the specified
portions to return immediately with an appropriate value (see the
documentation of Shutdown
).
sourcepub fn take_error(&mut self) -> Result<Option<Error>>
pub fn take_error(&mut self) -> Result<Option<Error>>
Get the value of the SO_ERROR
option on this socket.
This will retrieve the stored error in the underlying socket, clearing the field in the process. This can be useful for checking errors between calls.
Trait Implementations§
source§impl Evented for TcpStream
impl Evented for TcpStream
source§impl IntoRawFd for TcpStream
impl IntoRawFd for TcpStream
source§fn into_raw_fd(self) -> RawFd
fn into_raw_fd(self) -> RawFd
source§impl Read for TcpStream
impl Read for TcpStream
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, Global>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> 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 moresource§impl Write for TcpStream
impl Write for TcpStream
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
)