1. ホーム

エラー: 接続 ECONNREFUSED 111.231.137.44:6996

2022-02-21 16:54:25

問題点

NodeとのTCP接続が確立され、サーバーのポートが正常に開くが、そのサーバーの対応するポートにローカルリクエストを行うと、以下のエラーが報告される。

events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: connect ECONNREFUSED 111.231.137.44:6996
    at Object._errnoException (util.js:1024:11)
    at _exceptionWithHostPort (util.js:1046:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)

サーバーサイドのコードです。

var net = require('net');

var HOST = '127.0.0.1';
var PORT = 6969;

// Create a TCP server instance and call the listen function to start listening on the specified port
// The callback function passed to net.createServer() will be the handler for the "connection" event
// In each "connection" event, the socket object received by this callback function is unique
net.createServer(function(sock) {

    // We get a connection - the connection is automatically associated with a socket object
    console.log('CONNECTED: ' +
        sock.remoteAddress + ':' + sock.remotePort);

    // Add a "data" event handler to this socket instance
    sock.on('data', function(data) {
        console.log('DATA ' + sock.remoteAddress + ': ' + data);
        // send this data back and the client will receive the data from the server
        sock.write('You said "' + data + '"');
    });

    // Add a "close" event handler to this socket instance
    sock.on('close', function(data) {
        console.log('CLOSED: ' +
            sock.remoteAddress + ' ' + sock.remotePort);
    });

    // console.log('Server listening on ' + HOST +':' + PORT);

}).listen(PORT, HOST);

console.log('Server listening on ' + HOST +':' + PORT);

ローカルコードです。

var net = require('net');

var HOST = '111.231.137.44';
// var HOST = '127.0.0.1'
var PORT = 6969;

var client = new net.Socket();
client.connect(PORT, HOST, function() {

    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
    // Send data to the server as soon as the connection is established and the server will receive it 
    client.write('I am Chuck Norris!');

});

// Add a "data" event handler to the client
// data is the data sent back from the server
client.on('data', function(data) {

    console.log('DATA: ' + data);
    // Close the connection completely
    client.destroy();

});

// Add a "close" event handler to the client
client.on('close', function() {
    console.log('Connection closed');
});