繁体   English   中英

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

[英]node net socket timeout error on client

这是我在 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 [作为完成] net.js:1063:14) vivek@vivek-Latitude-E6220:/var/www/html/test/NODE/net$ node client.js events.js:141 throw er; // 未处理的“错误”事件^

错误:连接 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为您的应用程序分配的端口。 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');
});

客户端代码尝试连接到错误地址的服务器。 确保客户端代码的 IP 地址和端口号与服务器的 IP 地址和端口号匹配。 此外,请确保服务器正在运行并且服务器和客户端之间的网络连接已打开。 如果问题仍然存在,请尝试使用不同的端口号并确保该端口在服务器上可用。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM