pub struct TcpListener { /* private fields */ }Expand description
A TCP socket listener.
This works much like the TcpListener in the standard library, but this
doesn’t block when calling accept and instead returns a WouldBlock
error.
§Deregistering
TcpListener will deregister itself when dropped, iff it is not cloned
(via try_clone).
§Examples
use std::time::Duration;
use mio_st::event::{Events, EventedId};
use mio_st::net::TcpListener;
use mio_st::poll::{Poller, PollOption};
let address = "127.0.0.1:8001".parse()?;
let mut listener = TcpListener::bind(address)?;
let mut poller = Poller::new()?;
let mut events = Events::new();
// Register the socket with `Poller`
poller.register(&mut listener, EventedId(0), TcpListener::INTERESTS, PollOption::Edge)?;
poller.poll(&mut events, Some(Duration::from_millis(100)))?;
// There may be a socket ready to be accepted.Implementations§
Source§impl TcpListener
impl TcpListener
Sourcepub const INTERESTS: Interests = Interests::READABLE
pub const INTERESTS: Interests = Interests::READABLE
The interests to use when registering to receive acceptable connections events.
Sourcepub fn bind(address: SocketAddr) -> Result<TcpListener>
pub fn bind(address: SocketAddr) -> Result<TcpListener>
Convenience method to bind a new TCP listener to the specified address to receive new connections.
This also sets the SO_REUSEPORT and SO_REUSEADDR options on the
socket.
Sourcepub fn try_clone(&self) -> Result<TcpListener>
pub fn try_clone(&self) -> Result<TcpListener>
Create a independently owned handle to the underlying socket.
The returned TcpListener is a reference to the same socket as self.
Both handles can be used to accept incoming connections and options set
on one listener will affect the other.
§Notes
On Linux when a TcpListener is cloned it must deregistered. If its not
deregistered explicitly and one listener is closed (dropped) and onother
is still open the poller will still receive events.
Sourcepub fn accept(&mut self) -> Result<(TcpStream, SocketAddr)>
pub fn accept(&mut self) -> Result<(TcpStream, SocketAddr)>
Accepts a new TcpStream.
This may return an WouldBlock error, this means a stream may be
ready at a later point and one should wait for a notification before
calling accept again.
If an accepted stream is returned, the remote address of the peer is returned along with it.
Sourcepub fn local_addr(&mut self) -> Result<SocketAddr>
pub fn local_addr(&mut self) -> Result<SocketAddr>
Returns the local socket address of this listener.
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 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 AsRawFd for TcpListener
Available on Unix only.
impl AsRawFd for TcpListener
Source§impl Debug for TcpListener
impl Debug for TcpListener
Source§impl Evented for TcpListener
impl Evented for TcpListener
Source§impl FromRawFd for TcpListener
Available on Unix only.
impl FromRawFd for TcpListener
Source§unsafe fn from_raw_fd(fd: RawFd) -> TcpListener
unsafe fn from_raw_fd(fd: RawFd) -> TcpListener
The caller must ensure that the listener is in non-blocking mode when using this function.