使用 TcpStream 产生 10061 ConnectionRefused

Spawning with TcpStream leads to 10061 ConnectionRefused

我正在尝试制作一个可以处理多个流的简单主机(类似于 https://tokio.rs/tokio/tutorial 中给出的示例):

use std::{error::Error, time::Duration};
use tokio::{
    io::{AsyncReadExt, AsyncWriteExt},
    net::{TcpListener, TcpStream},
    time::sleep,
};

type GenericResult<T> = Result<T, Box<dyn Error>>;

#[tokio::main]
async fn main() {
    const ADDRESS: &str = "127.0.0.1:8080";
    let listener = TcpListener::bind(ADDRESS).await.unwrap();
    tokio::spawn(async { host(listener) });
    let mut stream = TcpStream::connect(ADDRESS).await.unwrap();
    stream.write_all(b"testing").await.unwrap();
}
async fn host(listener: TcpListener) -> GenericResult<()> {
    loop {
        let (stream, _) = listener.accept().await?;
        println!("new connection");
        tokio::spawn(async { process(stream).await.unwrap() });
    }
    async fn process(mut stream: TcpStream) -> GenericResult<()> {
        // Reads from stream
        let mut buffer = Vec::with_capacity(128);
        let mut position = 0;
        loop {
            // Read from stream into buffer
            let n = stream.read(&mut buffer[position..]).await?;
            // Advance position
            position += n;
            // Print buffer
            println!("buffer: {:?}", buffer);
            sleep(Duration::from_millis(100)).await;
        }
    }
}

但是当运行这个我遇到了:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 10061, kind: ConnectionRefused, message: "No connection could be made because the target machine actively refused it." }', src\main.rs:12:56
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

如有任何帮助,我们将不胜感激。

特别是为什么我在这里的实现与 https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=5b944a8c4703d438d48ef5e556f1fc08

中的有效实现不同(在功能上)

cargo --version --verbose:

cargo 1.60.0 (d1fd9fe2c 2022-03-01)
release: 1.60.0
commit-hash: d1fd9fe2c40a1a56af9132b5c92ab963ac7ae422
commit-date: 2022-03-01
host: x86_64-pc-windows-msvc
libgit2: 1.3.0 (sys:0.13.23 vendored)
libcurl: 7.80.0-DEV (sys:0.4.51+curl-7.80.0 vendored ssl:Schannel)
os: Windows 10.0.22000 (Windows 10 Pro) [64-bit]

tokio = {version="1.18.0",features=["full"]}

在开始收听之前定义流

use std::{error::Error, time::Duration};
use tokio::{
    io::{AsyncReadExt, AsyncWriteExt},
    net::{TcpListener, TcpStream},
    time::sleep,
};

type GenericResult<T> = Result<T, Box<dyn Error>>;

#[tokio::main]
async fn main() {
    const ADDRESS: &str = "127.0.0.1:8080";
    let listener = TcpListener::bind(ADDRESS).await.unwrap();
    let mut stream = TcpStream::connect(ADDRESS).await.unwrap();
    tokio::spawn(async { host(listener) });
    stream.write_all(b"testing").await.unwrap();
}
async fn host(listener: TcpListener) -> GenericResult<()> {
    loop {
        let (stream, _) = listener.accept().await?;
        println!("new connection");
        tokio::spawn(async { process(stream).await.unwrap() });
    }
    async fn process(mut stream: TcpStream) -> GenericResult<()> {
        // Reads from stream
        let mut buffer = Vec::with_capacity(128);
        let mut position = 0;
        loop {
            // Read from stream into buffer
            let n = stream.read(&mut buffer[position..]).await?;
            // Advance position
            position += n;
            // Print buffer
            println!("buffer: {:?}", buffer);
            sleep(Duration::from_millis(100)).await;
        }
    }
}