async_std::net::TcpListener does not work

Hello,

I try a very basic example of async_std. I probably do something wrong. Any help is welcome.

The code i am trying to do is:

    let listener = TcpListener::bind("127.0.0.1:4444").await.unwrap();
    println!("Server listening on {}", listener.local_addr().unwrap());

    listener
        .incoming()
        .for_each_concurrent(None, |tcpstream| async move {
            let tcpstream = tcpstream.unwrap();
            println!("Accepting from: {}", tcpstream.peer_addr().unwrap());
        })
        .await;

It compile well.
When i start this TCP listener, and connect to it:

nc 127.0.0.1 4444

I expect the ip:port to be prompted thanks to the println!
But there is nothing, like the code execute.
But i verify with wireshark that the TCP server responds to the nc connection.
So there is definitely something that goes wrong.

If you have an idea ... please let me know

That's a good question. I'm not sure why yours fail, but this works.

use async_std::net::TcpListener;

#[async_std::main]
async fn main() {
    let listener = TcpListener::bind("127.0.0.1:4444").await.unwrap();
    println!("Server listening on {}", listener.local_addr().unwrap());

    loop {
        let (tcp, ip) = listener.accept().await.unwrap();
        println!("Accepting from: {}", tcp.peer_addr().unwrap());
    }

}

Your original code works fine with Tokio.

use tokio::net::TcpListener;
use futures::StreamExt;

#[tokio::main]
async fn main() {
    let mut listener = TcpListener::bind("127.0.0.1:4444").await.unwrap();
    println!("Server listening on {}", listener.local_addr().unwrap());

    listener
        .incoming()
        .for_each_concurrent(None, |tcpstream| async move {
            let tcpstream = tcpstream.unwrap();
            println!("Accepting from: {}", tcpstream.peer_addr().unwrap());
        })
        .await;
}
1 Like

Thanks for raising this! A report was filed in async-std#888 and we've published async-std@1.6.5 with a fix.

The root cause of this issue was that one of our dependencies (async-io) published a new version earlier today that included a change in Drop behavior. On its own it was a perfectly reasonable change, but it ended up affecting our implementation causing a regression. But we've managed to resolve this now!

6 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.