ErrorKind::AddrInUse
RustERRORCommonNetwork

Address already in use

Quick Answer

Use SO_REUSEADDR, pick a different port, or wait for TIME_WAIT to expire.

What this means

The local address or port is already bound by another socket.

Why it happens
  1. 1Another process is listening on the same port
  2. 2Previous process crash left socket in TIME_WAIT
  3. 3Binding 0.0.0.0 conflicts with a specific-address bind

Fix

Enable SO_REUSEADDR with socket2

Enable SO_REUSEADDR with socket2
use socket2::{Socket, Domain, Type};
let sock = Socket::new(Domain::IPV4, Type::STREAM, None)?;
sock.set_reuse_address(true)?;
sock.bind(&"0.0.0.0:8080".parse::<std::net::SocketAddr>()?.into())?;

Why this works

SO_REUSEADDR allows binding a port in TIME_WAIT state.

Code examples
Triggerrust
TcpListener::bind("0.0.0.0:80")?; // port 80 already taken
Same error in other languages
Sources

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Rust errors