Struct TcpListener
pub struct TcpListener { /* private fields */ }
Available on crate feature
async-networking
only.Expand description
A TCP socket server, listening for connections.
You can accept a new connection by using the accept
method.
§Examples
use tokio_uring::net::TcpListener;
use tokio_uring::net::TcpStream;
let listener = TcpListener::bind("127.0.0.1:2345".parse().unwrap()).unwrap();
tokio_uring::start(async move {
let (tx_ch, rx_ch) = tokio::sync::oneshot::channel();
tokio_uring::spawn(async move {
let (rx, _) = listener.accept().await.unwrap();
if let Err(_) = tx_ch.send(rx) {
panic!("The receiver dropped");
}
});
tokio::task::yield_now().await; // Ensure the listener.accept().await has been kicked off.
let tx = TcpStream::connect("127.0.0.1:2345".parse().unwrap()).await.unwrap();
let rx = rx_ch.await.expect("The spawned task expected to send a TcpStream");
tx.write(b"test" as &'static [u8]).submit().await.0.unwrap();
let (_, buf) = rx.read(vec![0; 4]).await;
assert_eq!(buf, b"test");
});