gpt4 book ai didi

javascript - Node 'net' 模块 ECONNREFUSED 错误

转载 作者:行者123 更新时间:2023-11-30 12:43:14 25 4
gpt4 key购买 nike

我正在查看此页面: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

我尝试了很多不同的端口,我确定 Node 还没有运行

最佳答案

您尝试连接的网络服务器是否正在运行?

我试过了,它对我有用:

网络.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

关于javascript - Node 'net' 模块 ECONNREFUSED 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23600144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com