这是我的服务器端代码,托管在上,
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抛出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
当我在本地运行服务器代码时,它工作得很好。请帮我找出错误。
发布于 2016-01-13 17:38:02
您需要侦听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');
});发布于 2016-01-13 08:56:20
当客户端破坏套接字时,您的服务器中有一个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');
});https://stackoverflow.com/questions/34761853
复制相似问题