Node Net refuses connection no matter what on learnyounode

时间:2015-06-26 09:27:51

标签: node.js

I continuously get the following error:

✗ Error connecting to localhost:20496: connect ECONNREFUSED

note: The local host number is always changing.

On cloud9.ide the error happens before a timeout, and on my computer the error happens after a timeout. (it happens in both areas so I think it's a local host problem)

The code I'm using is exactly as follows:

var net = require('net');
function zero(i) {
  return (i < 10 ? '0' : '') + i;
}
function now () {
  var d = new Date();
  return d.getFullYear() + '-' + zero(d.getMonth()) + '-'
    + zero(d.getDate()) + ' ' + zero(d.getHours()) + ':'
    + zero(d.getMinutes());
}

var server = net.createServer(function (socket) {
    socket.error(function(){
        console.log("Error");
    });
  socket.end("FOUND:"+now() + '\n');
}).listen(8000);

I don't understand why the net module doesn't work and the http module did. I feel like it has something to do with the port being listened to, but I changed it to 3306 and there were no differences in the output.

I believe I'm running the latest version of node and learnyounode, and my OS is widnows7.

versions:

  • Npm:2.11.2
  • Node:0.12.5

1 个答案:

答案 0 :(得分:2)

你可以尝试这个(取自文档)并回复结果:

public class WorkChronometer {

    private DateTime startingInstant;
    private DateTime stoppingInstant;
    private Boolean  counting;

    //More methods

    public Integer getAccumulatedMinutes() {
        if (counting)
            throw new RuntimeException("Call stopCount first!");

        if (startingInstant == null || stoppingInstant == null)
            return 0;

        return Minutes.minutesBetween(startingInstant, stoppingInstant).getMinutes();
    }

    public Float getAccumulatedHours() {
        Integer accumulatedMinutes = getAccumulatedMinutes();
        return accumulatedMinutes / 60f;
    }
}

你最终会像这样测试它:

var net = require('net');
var server = net.createServer(function(c) { //'connection' listener
  console.log('client connected');
  c.on('end', function() {
    console.log('client disconnected');
  });
  c.write('hello\r\n');
  c.pipe(c);
});
server.listen(8124, function() { //'listening' listener
  console.log('server bound');
});
  

要收听套接字/tmp/echo.sock,最后一行的第三行将改为

     

telnet localhost 8124

     

使用nc连接到UNIX域套接字服务器:

     

server.listen('/tmp/echo.sock', function() { //'listening' listener