Connection refused (os error 111)


I’m trying to use TCP connections, but none of my local-hosts will establish a connection. A message error is sent to me (Connection refused (os error 111)).

Here’s the code:

use std::io::prelude::*;
use std::net::{TcpStream, TcpListener};

fn main() {
    let mut stream = match TcpStream::connect("") {
        Ok(stream) => stream,
        Err(e) => panic!("{}", e),

    let _ = stream.write(b"teste");

    let mut buffer: [u8; 32] = [0; 32];
    let _ = buffer);

    println!("{:?}", &buffer);

What is possibly happening here? My system is Linux arch 4.2.5-1-ARCH.


What user are you running this as?


It’s a normal user. This should work for every user, shouldn’t it?


You’re trying to bind to port 80, which needs root privileges. Have you tried running the server as a port >1024?


It seems that every port here needs root privileges. But I have other programs connected to local-host too, which aren’t running as super user.


It’s not clear what binding to any port has to do with this code.

If connection is refused it’s likely nothing is listening at that address. What is this code supposed to be talking to? Does netcat work?

echo -n teste | nc 80


It seems to work. Also, UDP sockets are okay here.