node 'net' module ECONNREFUSED error

情到浓时终转凉″ 提交于 2021-02-08 07:32:25

问题


I'm looking at this page: http://nodejs.org/api/net.html#net_net_createconnection_options_connectionlistener

Running the code from the page:

var net = require('net');
var client = net.connect({port: 8124},
    function() { //'connect' listener
  console.log('client connected');
  client.write('world!\r\n');
});
client.on('data', function(data) {
  console.log(data.toString());
  client.end();
});
client.on('end', function() {
  console.log('client disconnected');
});

and I'm getting the error:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: connect ECONNREFUSED
    at errnoException (net.js:901:11)
    at Object.afterConnect [as oncomplete] (net.js:892:19)

shell returned 8

version stuff:

~ % node --version
v0.10.25

~ % uname -a
Linux human1 3.13.0-031300-generic #201401192235 SMP Mon Jan 20 03:36:48 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

I've tried a lot of different ports and I'm positive node isn't already running


回答1:


Is the net server you're trying to connect to running?

I tried this and it works for me:

net.js

var net = require('net');

var server = net.createServer(function(client) {
    console.log('connected');
});

server.listen(8124);

var client = net.connect({port: 8124}, function() {
    console.log('client connected');
    client.write('world!\r\n');
});
client.on('data', function(data) {
    console.log(data.toString());
    client.end();
});
client.on('end', function() {
    console.log('client disconnected');
});

Run:

$ node net.js 
connected
client connected


来源:https://stackoverflow.com/questions/23600144/node-net-module-econnrefused-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!