1

I'm trying to listen on a tcp port that is sending continuous streams of data separated by curly braces { x, y, z}. When I run this script with node, I get the first console output "client connected", but the rest of the script hangs. Is there anything I can do to display the data as it arrives??

var s = require('net');

var client = s.connect(2324, '10.12.1.22', function(){
    console.log('client connected');

});

client.on('data', function(d){
      console.log(d);
});
client.end();
1
  • because you call client.end() before data can be received, you should put client.end(); inside the data event. and I believe you need s.close() or s.end() Commented Feb 26, 2014 at 22:27

1 Answer 1

1

You are emiting end event immediately, which means connection goes down instantly. In fact you should not emit end, TCP Socket is a Stream, it will emit end on data end. Take a look at net module API docs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.