ErrorKind::ConnectionRefused
RustERRORCommonNetwork
Connection refused
Quick Answer
Verify the server is running and listening on the correct host:port.
What this means
The remote host actively refused the TCP connection. The server is not listening on the requested port.
Why it happens
- 1Server process is not running
- 2Wrong port number
- 3Firewall blocking the port
Fix
Retry with backoff
Retry with backoff
use std::net::TcpStream;
use std::time::Duration;
use std::thread;
for attempt in 1..=3 {
match TcpStream::connect("127.0.0.1:8080") {
Ok(s) => { /* use stream */ break; }
Err(e) => { eprintln!("attempt {}: {}", attempt, e); thread::sleep(Duration::from_secs(1)); }
}
}Why this works
Retrying gives a briefly-starting server time to become ready.
Code examples
Triggerrust
TcpStream::connect("127.0.0.1:9999")?; // nothing listeningSame error in other languages
Sources
Official documentation ↗
Rust std::io
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev