Wrote tcp server via socket new net.Socket

 var net = require('net'); var clientSocket = new net.Socket; clientSocket.setEncoding('utf8'); clientSocket.connect({ port: 8080, host: 'localhost' }, function() { console.log('connected to server'); clientSocket.write('Hello \n'); }); clientSocket.on('data', function(data) { console.log(data); }); clientSocket.on('close', function() { console.log('Connection closed'); }); 

Displays error:

enter image description here

Where is the mistake ?

    1 answer 1

    The code you provided is the client code that is trying to connect to localhost. But it seems that nobody is waiting for him there. Therefore, the above error will be observed. The solution is to verify that the server really exists and is running on port 8080, also that it is listening on this port and the firewall has not banned it.