0
votes

This is my server side code which has been hosted on 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');
});

I am using below code as client on local,

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

When I run, It throws error Like.

events.js:141 throw er; // Unhandled 'error' event ^

Error: connect 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 throw er; // Unhandled 'error' event ^

Error: connect 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)

When I run the server code on local, It works perfect. Kindly help me to find the error.

2
Is the specified port open from Bluemix? Or is a server, client, or network firewall blocking the connection?GalacticCowboy
@GalacticCowboy. that particular port is not opened. I found that was the problem. But I didn't know how to open that port. so I hosted my app in aws and opened that port. Now it is working. If you know how to open the port on IBM Bluemix. Kindly post it as answer.Vivek Asai
I have no experience with Bluemix, sorry. I expect there is a way, but I don't know their system at all.GalacticCowboy

2 Answers

0
votes

You need to listen on the port that Bluemix assigns for your application. Bluemix will assign your application a port and you will need to bind on that port. Bluemix will load balance to your application and have your application available on ports 443 and 80.

You can get the port with the following code.

var port = process.env.PORT || 8124;

Also you don't need to bind to a host either.

I modified your code below.

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
votes

There is a read ECONNRESET Error in your server, when client destroy the socket.

you can catch using

c.on('error', function(err) {
    console.log('SOCKET  ERROR : ' , err);
});

you can avoid the crash this way.

working version for me, based on your code

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