Javascript 客户端上的节点网络套接字超时错误
这是我在IBM Bluemix上托管的服务器端代码Javascript 客户端上的节点网络套接字超时错误,javascript,node.js,sockets,websocket,ibm-cloud,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')
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.\u errnoException(util.js:856:11)
在导出时。主机端口例外(util.js:879:20)
在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
在Object.exports.\u errnoException(util.js:856:11)
在导出时。主机端口例外(util.js:879:20)
在TCPConnectWrap.afterConnect[as oncomplete](net.js:1063:14)
当我在本地运行服务器代码时,它工作得非常好。请帮助我查找错误。当客户端销毁套接字时,您的服务器中有一个
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');
});
您需要监听Bluemix为您的应用程序分配的端口。Bluemix将为您的应用程序分配一个端口,您需要在该端口上绑定。Bluemix将为您的应用程序实现负载平衡,并使您的应用程序在端口
443
和80
上可用
您可以使用以下代码获取端口
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');
});
谢谢你的回答。但我认为客户端并没有和服务器建立任何连接。在这种情况下,我们不能使用错误事件@Oxii在当地试用过,效果良好。我会用我在本地写的代码修改我的答案,它也对我有用。但在服务器(IBMBlueMix)上不是。指定的端口是否从Bluemix打开?还是服务器、客户端或网络防火墙阻止了连接?@GalacticCowboy。该特定端口未打开。我发现这就是问题所在。但我不知道如何打开那个端口。因此,我在aws中托管了我的应用程序并打开了该端口。现在它正在工作。如果您知道如何在IBMBlueMix上打开端口。请将其作为答案发布。对不起,我没有使用Bluemix的经验。我想有办法,但我根本不知道他们的系统。