Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node - ENONET when connecting to a socket

Executing this code:

var fs = require('fs');
var Socket = require('net').Socket;

var socket = new Socket();

console.log('connecting to: ' + server.host + ':' + server.port );

socket.connect( server.host, server.port );

socket.on('error', function(err) {

    console.log(arguments);
});

socket.on('connect', function() {
    console.log('connected');
});

socket.on('end', function() {
    console.log('socket ended');
});

Always throws this error:

{ '0': { [Error: connect ENOENT] code: 'ENOENT', errno: 'ENOENT', syscall: 'connect' } }

I'm on CloudLinux(x64) based shared hosting with SSH access.

like image 956
Memoria Avatar asked Jan 09 '14 09:01

Memoria


2 Answers

You have your host and port backwards. According to the documentation, you should be doing:

socket.connect(server.port, server.host);
like image 114
qubyte Avatar answered Dec 22 '22 05:12

qubyte


Server Side:-

var net = require('net');

var server = net.createServer(functi`on (socket){
  socket.write("hi\n");
  socket.write("you there\n");
  socket.on("data", function(dd) {
    console.log(data);
    });
  });


server.listen(8001);

Client side:-

var fs = require('fs');
var Sock = require('net');

var socket = Sock.Socket();

socket.connect(8001,"127.0.0.1", function() {

    console.log('connecting to: ' + server.host + ':' + server.port );
});

socket.on('connect', function() {
    console.log('connected');
});

socket.on('error', function(err) {

    console.log(arguments);
});

socket.on('end', function() {
    console.log('socket ended');
});
like image 28
Mitesh Sondhi Avatar answered Dec 22 '22 06:12

Mitesh Sondhi