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.
You have your host and port backwards. According to the documentation, you should be doing:
socket.connect(server.port, server.host);
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');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With