簡體   English   中英

在NODEJS中連接ECONNREFUSED

[英]connect ECONNREFUSED in NODEJS

運行文件example4.js來測試另一個文件example3.js時出現錯誤。

這是我的代碼:

**//file example 3**

var http = require('http');
var fs = require('fs');

// write out numbers
function writeNumbers(res) {

    var counter = 0;

    // increment global, write to client
    for (var i = 0; i<100; i++) {
        counter++;
        res.write(counter.toString() + '\n');
    }
}

// create http server
http.createServer(function (req, res) {

    var query = require('url').parse(req.url).query;
    var app = require('querystring').parse(query).file + ".txt";

    // content header
    res.writeHead(200, {'Content-Type': 'text/plain'});

    // write out numbers
    writeNumbers(res);

    // timer to open file and read contents
    setTimeout(function() {

        console.log('opening ' + app);
        // open and read in file contents
        fs.readFile(app, 'utf8', function(err, data) {
            if (err)
                res.write('Could not find or open file for reading\n');
            else {
                res.write(data);
            }
            // reponse is done
            res.end();
        });
    },2000);
}).listen(8382);

console.log('Server running at 8124/');


**//end of example 3**

**//file example 4**

var http = require('http');

//The url we want, plus the path and options we need
var options = {
    host: 'localhost',
    port: 8124,
    path: '/?file=secondary',
    method: 'GET'
};

var processPublicTimeline = function(response) {
    // finished? ok, write the data to a file
    console.log('finished request');
};

for (var i = 0; i < 2000; i++) {
    // make the request, and then end it, to close the connection
    http.request(options, processPublicTimeline).end();
}

錯誤是:

events.js:72
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
Process finished with exit code 8

有一個包含相關文件的附件(我已將其中兩個文件包含在內)。 有人知道什么問題嗎?

我的猜測是,您的客戶端代碼試圖打開2000個並發連接,因此您超出了操作系統對並發打開文件描述符的限制。 嘗試使用ulimit實用程序增加

在代碼中,您的服務器正在偵聽端口號:8382,而您的客戶端正在嘗試連接到端口號:8124,那里沒有服務器在監聽客戶端。 因此,您將獲得ECONNREFUSED。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM