【问题标题】:node net socket timeout error on client客户端上的节点网络套接字超时错误
【发布时间】:2016-04-18 03:24:18
【问题描述】:

这是我在 IBM Bluemix 上托管的服务器端代码,

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

我在本地使用下面的代码作为客户端,

var net = require('net');

var HOST = 'xxx.xx.xx.xx';
var PORT = xxxx;

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

    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
    // Write a message to the socket as soon as the client is connected, the server will receive it as message from the client
    client.write('I am Chuck Norris!');

});

// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
client.on('data', function(data) {

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

});

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

当我运行时,它会抛出错误。

events.js:141 投掷者; // 未处理的“错误”事件 ^

错误:连接 ETIMEDOUT xxx.xx.xx.xx:xxxx 在 Object.exports._errnoException (util.js:856:11) 在exports._exceptionWithHostPort (util.js:879:20) 在 TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14) vivek@vivek-Latitude-E6220:/var/www/html/test/NODE/net$ 节点 client.js 事件.js:141 投掷者; // 未处理的“错误”事件 ^

错误:连接 ETIMEDOUT xxxx.xx.xx.xx:xxxx 在 Object.exports._errnoException (util.js:856:11) 在exports._exceptionWithHostPort (util.js:879:20) 在 TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)

当我在本地运行服务器代码时,它运行良好。请帮我找出错误。

【问题讨论】:

  • 指定端口是否已从 Bluemix 打开?还是服务器、客户端或网络防火墙阻止了连接?
  • @GalacticCowboy。该特定端口未打开。我发现这就是问题所在。但我不知道如何打开那个端口。所以我在 aws 中托管了我的应用程序并打开了那个端口。现在它正在工作。如果您知道如何在 IBM Bluemix 上打开端口。请将其发布为答案。
  • 我没有使用 Bluemix 的经验,抱歉。我希望有办法,但我根本不知道他们的系统。

标签: javascript node.js sockets websocket ibm-cloud


【解决方案1】:

您需要侦听 Bluemix 为您的应用程序分配的端口。 Bluemix 将为您的应用程序分配一个端口,您需要在该端口上进行绑定。 Bluemix 将对您的应用程序进行负载平衡,并使您的应用程序在端口44380 上可用。

您可以使用以下代码获取端口。

var port = process.env.PORT || 8124;

您也不需要绑定到主机。

我在下面修改了你的代码。

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

【讨论】:

    【解决方案2】:

    当客户端销毁套接字时,您的服务器中出现read ECONNRESET 错误。

    你可以使用

    c.on('error', function(err) {
        console.log('SOCKET  ERROR : ' , err);
    });
    

    您可以通过这种方式避免崩溃。

    我的工作版本,基于你的代码

    server.js

    const net = require('net');
    
    var server = net.createServer(function(c) {
          console.log('client connected');
          c.on('end', function(c) {
            console.log('sendHomeKeytoIosDevice : ERROR : ' + c);
          });
          c.on('error', function(err) {
            console.log('sendHomeKeytoIosDevice : ERROR : ' + err);
          });
          c.write('hello\r\n');
          c.pipe(c);
    });
    
    
    server.listen(8124,function() {
      console.log('server bound');
    });
    

    客户端.js

    var net = require('net');
    
    var HOST = 'localhost';
    var PORT = 8124;
    
    var client = new net.Socket();
    client.connect(PORT, HOST, function() {
    
        console.log('CONNECTED TO: ' + HOST + ':' + PORT);
        // Write a message to the socket as soon as the client is connected, the server will receive it as message from the client
        client.write('I am Chuck Norris!');
    
    });
    
    // Add a 'data' event handler for the client socket
    // data is what the server sent to this socket
    client.on('data', function(data) {
    
        console.log('DATA: ' + data);
        // Close the client socket completely
        client.destroy();
    
    });
    
    // Add a 'close' event handler for the client socket
    client.on('close', function() {
        console.log('Connection closed');
    });
    

    【讨论】:

    • 感谢您的回答。但我认为客户端没有与服务器建立任何连接。在这种情况下,我们不能使用错误事件。 @Oxi
    • 我在本地尝试过,它的工作原理。我会用我做的代码修改我的答案
    • 在本地它也对我有用。但在服务器(IBM Bluemix)上却不是。
    猜你喜欢
    • 2015-06-04
    • 2012-01-20
    • 2017-09-05
    • 2011-07-31
    • 2017-06-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多