Getting "ECONNREFUSED" Error When Connecting a Socket on Another Host

Apparently I'm testing a simple TCP server that uses Node.js.

Server code and client code work well if they are on the same computer .

However, it seems that when I start the server on another machine and test to connect to the server from the client on another machine, I get an error, as shown below.

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

I tried to enter the server IP address or server domain name, but no luck.

Server code is similar to below (server started with root privileges ..)

 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 looks 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 computers? Should I run the server code as a production mode (if there is such a mode)? Or, is there a limit on the range of ports?

+8
source share
1 answer

Set the server binding to 0.0.0.0 and install the client to connect to the correct server IP address. If the server listens for 127.0.0.1, it will only accept connections from its local host.

+12
source share

All Articles