pub struct TcpListener { /* private fields */ }
Expand description
A structure representing a socket server
§Examples
use futures_net::driver::sys::{Poll, Token};
use futures_net::driver::sys::event::{Events, Ready, PollOpt};
use futures_net::driver::sys::net::TcpListener;
use std::time::Duration;
let listener = TcpListener::bind(&"127.0.0.1:34255".parse()?)?;
let poll = Poll::new()?;
let mut events = Events::with_capacity(128);
// Register the socket with `Poll`
poll.register(&listener, Token(0), Ready::readable(),
PollOpt::edge())?;
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. - Bind the socket to the specified address.
- Call
listen
on the socket to prepare it to receive new connections.
If fine-grained control over the binding and listening process for a
socket is desired then the net2::TcpBuilder
methods can be used in
combination with the TcpListener::from_listener
method to transfer
ownership into mio.
Sourcepub fn from_std(listener: TcpListener) -> Result<TcpListener>
pub fn from_std(listener: TcpListener) -> Result<TcpListener>
Creates a new TcpListener
from an instance of a
std::net::TcpListener
type.
This function will set the listener
provided into nonblocking mode on
Unix, and otherwise the stream will just be wrapped up in an mio stream
ready to accept new connections and become associated with an event
loop.
The address provided must be the address that the listener is bound to.
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 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 accept_std(&self) -> Result<(TcpStream, SocketAddr)>
pub fn accept_std(&self) -> Result<(TcpStream, SocketAddr)>
Accepts a new std::net::TcpStream
.
This method is the same as accept
, except that it returns a TCP socket
in blocking mode which isn’t bound to mio
. This can be later then
converted to a mio
type, if necessary.
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Returns the local socket address of this listener.
Sourcepub fn try_clone(&self) -> Result<TcpListener>
pub fn try_clone(&self) -> Result<TcpListener>
Creates a new independently owned handle to the underlying socket.
The returned TcpListener
is a reference to the same socket that this
object references. Both handles can be used to accept incoming
connections and options set on one listener will affect the other.
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 set_only_v6(&self, only_v6: bool) -> Result<()>
pub fn set_only_v6(&self, only_v6: bool) -> Result<()>
Sets the value for the IPV6_V6ONLY
option on this socket.
If this is set to true
then the socket is restricted to sending and
receiving IPv6 packets only. In this case two IPv4 and IPv6 applications
can bind the same port at the same time.
If this is set to false
then the socket can be used to send and
receive packets from an IPv4-mapped IPv6 address.
Sourcepub fn only_v6(&self) -> Result<bool>
pub fn only_v6(&self) -> Result<bool>
Gets the value of the IPV6_V6ONLY
option for this socket.
For more information about this option, see set_only_v6
.
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 AsRawFd for TcpListener
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
impl FromRawFd for TcpListener
Source§unsafe fn from_raw_fd(fd: RawFd) -> TcpListener
unsafe fn from_raw_fd(fd: RawFd) -> TcpListener
Self
from the given raw file
descriptor. Read more