ErrorKind::NotConnected
RustERRORNotableNetwork
Not connected
Quick Answer
Ensure connect() succeeds before reading or writing.
What this means
A send or receive was attempted on a socket that is not connected.
Why it happens
- 1Reading from a UdpSocket before calling connect()
- 2Using a socket after it was closed
Fix
Connect before use
Connect before use
use std::net::UdpSocket;
let sock = UdpSocket::bind("0.0.0.0:0")?;
sock.connect("8.8.8.8:53")?; // required before send/recv
sock.send(b"query")?;Why this works
connect() sets the default peer address for send/recv on UDP sockets.
Code examples
Triggerrust
let s = UdpSocket::bind("0.0.0.0:0")?;
s.recv(&mut buf)?; // NotConnectedSame 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