Connection refused (os error 111)


#1

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("127.0.0.1:80") {
        Ok(stream) => stream,
        Err(e) => panic!("{}", e),
    };

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

    let mut buffer: [u8; 32] = [0; 32];
    let _ = stream.read(&mut buffer);

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

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


#2

What user are you running this as?


#3

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


#4

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


#5

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.


#6

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 127.0.0.1 80

#7

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