Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting 'ECONNREFUSED' error when socket connection is established on different host

Tags:

node.js

Apparently, I am testing out simple TCP server that uses Node.js.

The server code, and the client code works well if they are both on the same machine.

However, it seems that when I run the server on the different machine and test to connect to the server from the client in different machine, I get error like below.

Error: connect ECONNREFUSED
at errnoException (net.js:589:11)
at Object.afterConnect [as oncomplete] (net.js:580:18)

I tried by typing IP address of the server, or the domain name of the server, but no luck.

The server code is like below, (server was ran with root privilege..)

var net = require('net');
var HOST = '127.0.0.1';
var PORT = 6969;
net.createServer(function(sock) {
    console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);
    sock.on('data', function(data) {

        console.log('DATA ' + sock.remoteAddress + ': ' + data);
        sock.write('You said "' + data + '"');

    });
    sock.on('close', function(data) {
        console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
    });

}).listen(PORT, HOST);

console.log('Server listening on ' + HOST +':'+ PORT);

and the client code is like below

var net = require('net');
var HOST = '127.0.0.1'; //I set it to server IP address but no luck.. 
var PORT = 6969;
var client = new net.Socket();
client.connect(PORT, HOST, function() {
    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
    client.write('I am Chuck Norris!');
});
client.on('data', function(data) {        
    console.log('DATA: ' + data);
    client.destroy();
});
client.on('close', function() {
    console.log('Connection closed');
});

Is there any configuration that I have to go through, if I want the server to accept socket connections from different machine? Do I have to run server code as production mode (if there is such mode)?? Or, is there limitation in port range?

like image 524
user482594 Avatar asked Oct 31 '11 01:10

user482594


People also ask

What does Econnrefused mean?

Enhancement Number. The ECONNREFUSED Connection refused by the server error, is a common error returned by the Filezilla FTP client. This error indicates that the user is trying to connect to your server and is unable to connect to the port. There are a few reasons the client would receive this error.

How do I fix Econnreset error read?

I solved the problem by simply connecting to a different network. That is one of the possible problems. As discussed above, ECONNRESET means that the TCP conversation abruptly closed its end of the connection. Your internet connection might be blocking you from connecting to some servers.

What is connection refused error in Python?

This error means that for whatever reason the client cannot connect to the port on the computer running server script. This can be caused by few things, like lack of routing to the destination, but since you can ping the server, it should not be the case.


1 Answers

Set the server to bind to 0.0.0.0 and set the client to connect to the correct IP address of the server. If the server is listening on 127.0.0.1, it will only accept connections from its local host.

like image 192
David Schwartz Avatar answered Oct 01 '22 03:10

David Schwartz