pub struct TcpListener { /* private fields */ }
Expand description
A TCP socket server, listening for connections.
The socket will be closed when the value is dropped.
The accept and the incoming methods can be interrupted using the Canceller object created with the socket.
Otherwise similar to std::net::TcpListener.
Implementations§
Source§impl TcpListener
impl TcpListener
Sourcepub fn bind<A: ToSocketAddrs>(address: A) -> Result<(Self, Canceller)>
pub fn bind<A: ToSocketAddrs>(address: A) -> Result<(Self, Canceller)>
Creates a new TcpListener which will be bound to the specified address, together with an object that allows cancelling accept operations.
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
fn main() {
let (listener, listener_canceller) = TcpListener::bind("127.0.0.1:0").unwrap();
let address = listener.local_addr().unwrap();
let server = thread::spawn(move || {
println!("Server: ready");
let (mut socket, socket_canceller, addr) = listener.accept().unwrap();
println!("Server: got connection from {}", addr);
let connection = thread::spawn(move || {
println!("Connection: ready");
let mut buf = [0; 16];
let len = socket.read(&mut buf).unwrap();
println!("Connection: read {}", String::from_utf8_lossy(&buf[..len]));
println!("Connection: try reading more.");
match socket.read(&mut buf) {
Ok(0) => println!("Connection: socket closed."),
Err(ref e) if is_cancelled(e) => println!("Connection: cancelled."),
ref e => panic!("Connection: unexpected {:?}", e),
}
});
println!("Server: try accepting more.");
if is_cancelled(&listener.accept().unwrap_err()) {
println!("Server: accept cancelled.");
}
socket_canceller.cancel().unwrap();
connection.join().unwrap();
});
thread::sleep(Duration::from_secs(2));
let (mut socket, socket_canceller) = TcpStream::connect(&address).unwrap();
let client = thread::spawn(move || {
println!("Client: ready");
thread::sleep(Duration::from_secs(2));
println!("Client: write data.");
socket.write(b"Hello!").unwrap();
println!("Client: try reading.");
let mut buf = [0; 16];
match socket.read(&mut buf) {
Ok(0) => println!("Client: socket closed."),
Err(ref e) if is_cancelled(e) => println!("Client: cancelled."),
ref e => panic!("Client: unexpected {:?}", e),
}
});
thread::sleep(Duration::from_secs(4));
socket_canceller.cancel().unwrap();
thread::sleep(Duration::from_secs(2));
listener_canceller.cancel().unwrap();
server.join().unwrap();
client.join().unwrap();
}
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Returns the local socket address of this listener.
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
fn main() {
let (listener, listener_canceller) = TcpListener::bind("127.0.0.1:0").unwrap();
let address = listener.local_addr().unwrap();
let server = thread::spawn(move || {
println!("Server: ready");
let (mut socket, socket_canceller, addr) = listener.accept().unwrap();
println!("Server: got connection from {}", addr);
let connection = thread::spawn(move || {
println!("Connection: ready");
let mut buf = [0; 16];
let len = socket.read(&mut buf).unwrap();
println!("Connection: read {}", String::from_utf8_lossy(&buf[..len]));
println!("Connection: try reading more.");
match socket.read(&mut buf) {
Ok(0) => println!("Connection: socket closed."),
Err(ref e) if is_cancelled(e) => println!("Connection: cancelled."),
ref e => panic!("Connection: unexpected {:?}", e),
}
});
println!("Server: try accepting more.");
if is_cancelled(&listener.accept().unwrap_err()) {
println!("Server: accept cancelled.");
}
socket_canceller.cancel().unwrap();
connection.join().unwrap();
});
thread::sleep(Duration::from_secs(2));
let (mut socket, socket_canceller) = TcpStream::connect(&address).unwrap();
let client = thread::spawn(move || {
println!("Client: ready");
thread::sleep(Duration::from_secs(2));
println!("Client: write data.");
socket.write(b"Hello!").unwrap();
println!("Client: try reading.");
let mut buf = [0; 16];
match socket.read(&mut buf) {
Ok(0) => println!("Client: socket closed."),
Err(ref e) if is_cancelled(e) => println!("Client: cancelled."),
ref e => panic!("Client: unexpected {:?}", e),
}
});
thread::sleep(Duration::from_secs(4));
socket_canceller.cancel().unwrap();
thread::sleep(Duration::from_secs(2));
listener_canceller.cancel().unwrap();
server.join().unwrap();
client.join().unwrap();
}
Sourcepub fn try_clone(&self) -> Result<(Self, Canceller)>
pub fn try_clone(&self) -> Result<(Self, Canceller)>
Creates a new independently owned handle to the underlying socket.
Sourcepub fn accept(&self) -> Result<(TcpStream, Canceller, SocketAddr)>
pub fn accept(&self) -> Result<(TcpStream, Canceller, SocketAddr)>
Accepts a new incoming connection from this listener.
This method can be cancelled by the associated Canceller object.
This method returns a TcpStream with an associated Canceller and the address of the remote peer.
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
fn main() {
let (listener, listener_canceller) = TcpListener::bind("127.0.0.1:0").unwrap();
let address = listener.local_addr().unwrap();
let server = thread::spawn(move || {
println!("Server: ready");
let (mut socket, socket_canceller, addr) = listener.accept().unwrap();
println!("Server: got connection from {}", addr);
let connection = thread::spawn(move || {
println!("Connection: ready");
let mut buf = [0; 16];
let len = socket.read(&mut buf).unwrap();
println!("Connection: read {}", String::from_utf8_lossy(&buf[..len]));
println!("Connection: try reading more.");
match socket.read(&mut buf) {
Ok(0) => println!("Connection: socket closed."),
Err(ref e) if is_cancelled(e) => println!("Connection: cancelled."),
ref e => panic!("Connection: unexpected {:?}", e),
}
});
println!("Server: try accepting more.");
if is_cancelled(&listener.accept().unwrap_err()) {
println!("Server: accept cancelled.");
}
socket_canceller.cancel().unwrap();
connection.join().unwrap();
});
thread::sleep(Duration::from_secs(2));
let (mut socket, socket_canceller) = TcpStream::connect(&address).unwrap();
let client = thread::spawn(move || {
println!("Client: ready");
thread::sleep(Duration::from_secs(2));
println!("Client: write data.");
socket.write(b"Hello!").unwrap();
println!("Client: try reading.");
let mut buf = [0; 16];
match socket.read(&mut buf) {
Ok(0) => println!("Client: socket closed."),
Err(ref e) if is_cancelled(e) => println!("Client: cancelled."),
ref e => panic!("Client: unexpected {:?}", e),
}
});
thread::sleep(Duration::from_secs(4));
socket_canceller.cancel().unwrap();
thread::sleep(Duration::from_secs(2));
listener_canceller.cancel().unwrap();
server.join().unwrap();
client.join().unwrap();
}
Sourcepub fn incoming(&self) -> Incoming<'_> ⓘ
pub fn incoming(&self) -> Incoming<'_> ⓘ
Returns an iterator over the connections being received by this listener.
The iteration can be cancelled by the associated Canceller object. If the iteration is cancelled, the next() method will return an error that can be identified with is_cancelled.
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.
Sourcepub fn take_error(&self) -> Result<Option<Error>>
pub fn take_error(&self) -> Result<Option<Error>>
Gets the value for the SO_ERROR
option for this socket.
Sourcepub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
Moves this TCP connection into or out of non blocking mode.