首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >客户端节点网络套接字超时错误

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

Stack Overflow用户
提问于 2016-01-13 08:37:16
回答 2查看 2.7K关注 0票数 0

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

代码语言:javascript
复制
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');
});

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

代码语言:javascript
复制
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抛出er;//未处理的“错误”事件^ 错误:连接ETIMEDOUT xxx.xx.xx.xx:xxxx at Object.exports._errnoException (util.js:856:11) at exports._exceptionWithHostPort (util.js:879:20)在TCPConnectWrap.afterConnect as oncomplete节点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

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

EN

回答 2

Stack Overflow用户

发布于 2016-01-13 17:38:02

您需要侦听Bluemix为您的应用程序指定的端口。Bluemix将为您的应用程序分配一个端口,您将需要绑定在该端口上。Bluemix将向您的应用程序加载平衡,并且您的应用程序可以在端口44380上使用。

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

代码语言:javascript
复制
var port = process.env.PORT || 8124;

而且,您也不需要绑定到主机。

我修改了你下面的代码。

代码语言:javascript
复制
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');
});
票数 0
EN

Stack Overflow用户

发布于 2016-01-13 08:56:20

当客户端破坏套接字时,您的服务器中有一个read ECONNRESET错误。

你可以抓住使用

代码语言:javascript
复制
c.on('error', function(err) {
    console.log('SOCKET  ERROR : ' , err);
});

这样你就能避免坠机了。

为我工作的版本,基于您的代码

server.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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');
});
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34761853

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档