D
D
Dvorak2016-06-05 19:00:35
JavaScript
Dvorak, 2016-06-05 19:00:35

Why can't create a TCP client?

var net = require('net'),
    clientSocket = new net.Socket();

clientSocket.setEncoding('utf8');

clientSocket.connect('8080', 'localhost', function(){
  console.log('Connected!');
  clientSocket.write('Hello!');
});

clientSocket.on('data', function(data) {
  console.log('data: ' + data);
});

clientSocket.on('close', function() {
  console.log('Bye!');
});

Empirically revealed that the error is in the call to the connect () method. But where exactly, I can not understand

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2016-06-05
Protko @Fesor

There, in the function callback, there is an argument responsible for describing errors. Use it to understand why.
Also make sure that the server is up on the right port and on the right host. Well, that he is raised. The easiest way is through netcat.

R
Rou1997, 2016-06-05
@Rou1997

If in connect, then either in the IP address, try '127.0.0.1', or in the port, but rather the first, you can try to do the same in some other language, this will help "localize" the problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question