std:đ„ :TcpListener - Rust (original) (raw)
Struct std::net::TcpListener1.0.0 [â] [src]
pub struct TcpListener(_);
A TCP socket server, listening for connections.
After creating a TcpListener
by binding it to a socket address, it listens for incoming TCP connections. These can be accepted by calling accept or by iterating over the Incoming iterator returned by incoming.
The socket will be closed when the value is dropped.
The Transmission Control Protocol is specified in IETF RFC 793.
use std:đ„ :{TcpListener, TcpStream};
fn handle_client(stream: TcpStream) {
}
let listener = TcpListener::bind("127.0.0.1:80").unwrap();
for stream in listener.incoming() { handle_client(stream?); }Run
impl [TcpListener](../../std/net/struct.TcpListener.html "struct std:đ„
:TcpListener")
[src]
pub fn [bind](#method.bind)<A: [ToSocketAddrs](../../std/net/trait.ToSocketAddrs.html "trait std:đ„
:ToSocketAddrs")>(addr: A) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[TcpListener](../../std/net/struct.TcpListener.html "struct std:đ„
:TcpListener")>
[src]
Creates a new TcpListener
which will be bound to the specified address.
The returned listener is ready for accepting connections.
Binding with a port number of 0 will request that the OS assigns a port to this listener. The port allocated can be queried via thelocal_addr method.
The address type can be any implementor of ToSocketAddrs trait. See its documentation for concrete examples.
If addr
yields multiple addresses, bind
will be attempted with each of the addresses until one succeeds and returns the listener. If none of the addresses succeed in creating a listener, the error returned from the last attempt (the last address) is returned.
Create a TCP listener bound to 127.0.0.1:80
:
use std:đ„ :TcpListener;
let listener = TcpListener::bind("127.0.0.1:80").unwrap();Run
Create a TCP listener bound to 127.0.0.1:80
. If that fails, create a TCP listener bound to 127.0.0.1:443
:
use std:đ„ :{SocketAddr, TcpListener};
let addrs = [ SocketAddr::from(([127, 0, 0, 1], 80)), SocketAddr::from(([127, 0, 0, 1], 443)), ]; let listener = TcpListener::bind(&addrs[..]).unwrap();Run
pub fn [local_addr](#method.local%5Faddr)(&self) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[SocketAddr](../../std/net/enum.SocketAddr.html "enum std:đ„
:SocketAddr")>
[src]
Returns the local socket address of this listener.
use std:đ„ :{Ipv4Addr, SocketAddr, SocketAddrV4, TcpListener};
let listener = TcpListener::bind("127.0.0.1:8080").unwrap(); assert_eq!(listener.local_addr().unwrap(), SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)));Run
pub fn [try_clone](#method.try%5Fclone)(&self) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[TcpListener](../../std/net/struct.TcpListener.html "struct std:đ„
:TcpListener")>
[src]
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.
use std:đ„ :TcpListener;
let listener = TcpListener::bind("127.0.0.1:8080").unwrap(); let listener_clone = listener.try_clone().unwrap();Run
pub fn [accept](#method.accept)(&self) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[(](../primitive.tuple.html)[TcpStream](../../std/net/struct.TcpStream.html "struct std:đ„
:TcpStream"), [SocketAddr](../../std/net/enum.SocketAddr.html "enum std:đ„
:SocketAddr")[)](../primitive.tuple.html)>
[src]
Accept a new incoming connection from this listener.
This function will block the calling thread until a new TCP connection is established. When established, the corresponding TcpStream and the remote peer's address will be returned.
use std:đ„ :TcpListener;
let listener = TcpListener::bind("127.0.0.1:8080").unwrap(); match listener.accept() { Ok((_socket, addr)) => println!("new client: {:?}", addr), Err(e) => println!("couldn't get client: {:?}", e), }Run
pub fn [incoming](#method.incoming)(&self) -> [Incoming](../../std/net/struct.Incoming.html "struct std:đ„
:Incoming")
[src]
Returns an iterator over the connections being received on this listener.
The returned iterator will never return None and will also not yield the peer's SocketAddr structure. Iterating over it is equivalent to calling accept in a loop.
use std:đ„ :TcpListener;
let listener = TcpListener::bind("127.0.0.1:80").unwrap();
for stream in listener.incoming() { match stream { Ok(stream) => { println!("new client!"); } Err(e) => { } } }Run
pub fn [set_ttl](#method.set%5Fttl)(&self, ttl: [u32](../primitive.u32.html)) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[()](../primitive.unit.html)>
1.9.0
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.
use std:đ„ :TcpListener;
let listener = TcpListener::bind("127.0.0.1:80").unwrap(); listener.set_ttl(100).expect("could not set TTL");Run
pub fn [ttl](#method.ttl)(&self) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[u32](../primitive.u32.html)>
1.9.0
Gets the value of the IP_TTL
option for this socket.
For more information about this option, see set_ttl.
use std:đ„ :TcpListener;
let listener = TcpListener::bind("127.0.0.1:80").unwrap(); listener.set_ttl(100).expect("could not set TTL"); assert_eq!(listener.ttl().unwrap_or(0), 100);Run
pub fn [set_only_v6](#method.set%5Fonly%5Fv6)(&self, only_v6: [bool](../primitive.bool.html)) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[()](../primitive.unit.html)>
1.9.0
Deprecated since 1.16.0
: this option can only be set before the socket is bound
pub fn [only_v6](#method.only%5Fv6)(&self) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[bool](../primitive.bool.html)>
1.9.0
Deprecated since 1.16.0
: this option can only be set before the socket is bound
pub fn [take_error](#method.take%5Ferror)(&self) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[Option](../../std/option/enum.Option.html "enum std::option::Option")<[Error](../../std/io/struct.Error.html "struct std::io::Error")>>
1.9.0
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.
use std:đ„ :TcpListener;
let listener = TcpListener::bind("127.0.0.1:80").unwrap(); listener.take_error().expect("No error was expected");Run
pub fn [set_nonblocking](#method.set%5Fnonblocking)(&self, nonblocking: [bool](../primitive.bool.html)) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[()](../primitive.unit.html)>
1.9.0
Moves this TCP stream into or out of nonblocking mode.
This will result in the accept
operation becoming nonblocking, i.e. immediately returning from their calls. If the IO operation is successful, Ok
is returned and no further action is required. If the IO operation could not be completed and needs to be retried, an error with kind io::ErrorKind::WouldBlock is returned.
On Unix platforms, calling this method corresponds to calling fcntl
FIONBIO
. On Windows calling this method corresponds to callingioctlsocket
FIONBIO
.
Bind a TCP listener to an address, listen for connections, and read bytes in nonblocking mode:
use std::io; use std:đ„ :TcpListener;
let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); listener.set_nonblocking(true).expect("Cannot set non-blocking");
for stream in listener.incoming() { match stream { Ok(s) => {
handle_connection(s);
}
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
wait_for_fd();
continue;
}
Err(e) => panic!("encountered IO error: {}", e),
}
}Run
impl [Debug](../../std/fmt/trait.Debug.html "trait std::fmt::Debug") for [TcpListener](../../std/net/struct.TcpListener.html "struct std:đ„
:TcpListener")
[src]
impl [AsRawFd](../../std/os/unix/io/trait.AsRawFd.html "trait std::os::unix::io::AsRawFd") for [TcpListener](../../std/net/struct.TcpListener.html "struct std:đ„
:TcpListener")
[src]
impl [FromRawFd](../../std/os/unix/io/trait.FromRawFd.html "trait std::os::unix::io::FromRawFd") for [TcpListener](../../std/net/struct.TcpListener.html "struct std:đ„
:TcpListener")
1.1.0
impl [IntoRawFd](../../std/os/unix/io/trait.IntoRawFd.html "trait std::os::unix::io::IntoRawFd") for [TcpListener](../../std/net/struct.TcpListener.html "struct std:đ„
:TcpListener")
1.4.0
impl [AsRawSocket](../../std/os/windows/io/trait.AsRawSocket.html "trait std::os::windows::io::AsRawSocket") for [TcpListener](../../std/net/struct.TcpListener.html "struct std:đ„
:TcpListener")
[src]
impl [FromRawSocket](../../std/os/windows/io/trait.FromRawSocket.html "trait std::os::windows::io::FromRawSocket") for [TcpListener](../../std/net/struct.TcpListener.html "struct std:đ„
:TcpListener")
1.1.0
impl [IntoRawSocket](../../std/os/windows/io/trait.IntoRawSocket.html "trait std::os::windows::io::IntoRawSocket") for [TcpListener](../../std/net/struct.TcpListener.html "struct std:đ„
:TcpListener")
1.4.0