Error Connection Refused

What can be the reasons of connection refused errors?

There could be many reasons, but the most common are:

  1. The port is not open on the destination machine.

  2. The port is open on the destination machine, but its backlog of pending connections is full.

  3. A firewall between the client and server is blocking access (also check local firewalls).

After checking for firewalls and that the port is open, use telnet to connect to the ip/port to test connectivity. This removes any potential issues from your application.

SocketException: OS Error: Connection refused, errno = 111 in flutter using django backend

Harnish, need a few more details in-order to debug this error.

  1. Are you running the server locally or communicating with a remote server?
  2. Are you running the app on the Android Emulator?

Possible Solution:

If you're running the server locally and using the Android emulator, then your server endpoint should be 10.0.2.2:8000 instead of localhost:8000 as AVD uses 10.0.2.2 as an alias to your host loopback interface (i.e) localhost

Note on Futures

I noticed above that the code is using await and then on the same line. This can be confusing, to be clear, await is used to suspend execution until a future completes, and then is a callback function to execute after a future completed. The same could be written as below

void myFunction() async {
var data = {};
var response = await http.post(URL, headers:headers, body:data);
if (response.statusCode == 200) {
print(reponse.body);
} else {
print('A network error occurred');
}
}

or the non async/await method

void myFunction() {
var data = {};
http.post(URL, headers:headers, body:data)
.then((response) => print(response.body))
.catchError((error) => print(error));
}

For a more detailed information on Futures in Dart please read
https://www.dartlang.org/tutorials/language/futures

SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = 127.0.0.1, port = 44164

This happens because the localhost (or 127.0.0.1) on the device is only accessible to the device itself.

Solution 1

You can reverse-proxy a localhost port to the Android device/emulator running adb reverse on the command prompt like so:

adb reverse tcp:5000 tcp:5000

Solution 2

Use the machine's IP address where the API is running. Also, the API should be listening to the IP 0.0.0.0 to be accessible outside the localhost.

Supposing the API machine's IP is 192.168.1.123 it's going to be something like:

Uri.parse('http://192.168.1.123:5000/addrec')

Just take care because changing the API to listen to 0.0.0.0 is a security risk as the API is going to be accessible to the outside world.

apache node js socket.io error connection refused

Just had to replace

let socket = io.connect( 'http://localhost:3000' ); 

with

let socket = io.connect( 'http://192.168.100.31:3000/' );

java.net.ConnectException: Connection refused

This exception means that there is no service listening on the IP/port you are trying to connect to:

  • You are trying to connect to the wrong IP/Host or port.
  • You have not started your server.
  • Your server is not listening for connections.
  • On Windows servers, the listen backlog queue is full.


Related Topics



Leave a reply



Submit