客户端上的节点网络套接字超时错误

时间:2016-01-13 08:37:16

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

这是我在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       at Object.exports._errnoException(util.js:856:11)       at exports._exceptionWithHostPort(util.js:879:20)       at TCPConnectWrap.afterConnect [as oncomplete](net.js:1063:14)vivek @ vivek-Latitude-E6220:/ var / www / html / test / NODE / net $ node client.js   events.js:141         扔掉//未处理的'错误'事件         ^

     

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

当我在本地运行服务器代码时,它工作得很完美。请帮我找出错误。

2 个答案:

答案 0 :(得分:0)

您需要侦听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');
});

答案 1 :(得分:-1)

当客户端销毁套接字时,服务器中存在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');
});

Client.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');
});