Search code examples
rustrust-tokio

Why does the Tokio Hello World panic with "No connection could be made because the target machine actively refused it"?


This is the sample hello world program from the Tokio documentation .

use tokio::prelude::*;

#[tokio::main]
async fn main() {
    let mut stream = TcpStream::connect("127.0.0.1:6142").await.unwrap();
    println!("created stream");

    let result = stream.write(b"hello world\n").await;
    println!("wrote to stream; success={:?}", result.is_ok());
}

Which gives me this error:

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\libcore\result.rs:1165:5
stack backtrace:
.
.
.

How can I fix it?


Solution

  • You seemingly didn't follow the instructions for the hello world, thus you get an error because there's no server listening:

    Install socat, which is a network utility that we’ll use to simulate a server. Then type the following command to print out everything that is received on port 6142 (a somewhat arbitrary number we have chosen for this example):

    socat TCP-LISTEN:6142,fork stdout