我正在查看此页面:http://nodejs.org/api/net.html#net_net_createconnection_options_connectionlistener
从页面运行代码:
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');
});
我收到了错误:
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
版本的东西:
~ % 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
我尝试了很多不同的端口,而我的正节点尚未运行
答案 0 :(得分:3)
您尝试连接的网络服务器是否正在运行?
我尝试了这个,它对我有用:
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');
});
执行命令
$ node net.js
connected
client connected