pub struct TcpListener { /* private fields */ }
net
only.Expand description
A structure representing a socket server
§Examples
use mio::{Events, Interest, Poll, Token};
use mio::net::TcpListener;
use std::time::Duration;
let mut listener = TcpListener::bind("127.0.0.1:34255".parse()?)?;
let mut poll = Poll::new()?;
let mut events = Events::with_capacity(128);
// Register the socket with `Poll`
poll.registry().register(&mut listener, Token(0), Interest::READABLE)?;
poll.poll(&mut events, Some(Duration::from_millis(100)))?;
// There may be a socket ready to be accepted
Implementations§
Source§impl TcpListener
impl TcpListener
Sourcepub fn bind(addr: SocketAddr) -> Result<TcpListener>
pub fn bind(addr: SocketAddr) -> Result<TcpListener>
Convenience method to bind a new TCP listener to the specified address to receive new connections.
This function will take the following steps:
- Create a new TCP socket.
- Set the
SO_REUSEADDR
option on the socket on Unix. - Bind the socket to the specified address.
- Calls
listen
on the socket to prepare it to receive new connections.
Sourcepub fn from_std(listener: TcpListener) -> TcpListener
pub fn from_std(listener: TcpListener) -> TcpListener
Creates a new TcpListener
from a standard net::TcpListener
.
This function is intended to be used to wrap a TCP listener from the standard library in the Mio equivalent. The conversion assumes nothing about the underlying listener; ; it is left up to the user to set it in non-blocking mode.
Sourcepub fn accept(&self) -> Result<(TcpStream, SocketAddr)>
pub fn accept(&self) -> Result<(TcpStream, SocketAddr)>
Accepts a new TcpStream
.
This may return an Err(e)
where e.kind()
is
io::ErrorKind::WouldBlock
. This means a stream may be ready at a later
point and one should wait for an event 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(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Returns the local socket address of this listener.
Sourcepub fn set_ttl(&self, ttl: u32) -> Result<()>
pub fn set_ttl(&self, ttl: u32) -> Result<()>
Sets the value for the IP_TTL
option on this socket.
This value sets the time-to-live field that is used in every packet sent from this socket.
Sourcepub fn ttl(&self) -> Result<u32>
pub fn ttl(&self) -> Result<u32>
Gets the value of the IP_TTL
option for this socket.
For more information about this option, see set_ttl
.
Sourcepub fn take_error(&self) -> Result<Option<Error>>
pub fn take_error(&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 AsFd for TcpListener
impl AsFd for TcpListener
Source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
Source§impl AsRawFd for TcpListener
impl AsRawFd for TcpListener
Source§impl Debug for TcpListener
impl Debug for TcpListener
Source§impl From<TcpListener> for TcpListener
impl From<TcpListener> for TcpListener
Source§fn from(listener: TcpListener) -> Self
fn from(listener: TcpListener) -> Self
Source§impl FromRawFd for TcpListener
impl FromRawFd for TcpListener
Source§unsafe fn from_raw_fd(fd: RawFd) -> TcpListener
unsafe fn from_raw_fd(fd: RawFd) -> TcpListener
Converts a RawFd
to a TcpListener
.
§Notes
The caller is responsible for ensuring that the socket is in non-blocking mode.
Source§impl IntoRawFd for TcpListener
impl IntoRawFd for TcpListener
Source§fn into_raw_fd(self) -> RawFd
fn into_raw_fd(self) -> RawFd
Auto Trait Implementations§
impl !Freeze for TcpListener
impl RefUnwindSafe for TcpListener
impl Send for TcpListener
impl Sync for TcpListener
impl Unpin for TcpListener
impl UnwindSafe for TcpListener
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 16 bytes