1

I am issuing newline-separated text commands to a custom protocol TCP server. In the example below I issue 2 commands and receive a response written back. It works as expected in telnet and netcat:

$ nc localhost 1234
command1
command2
theresponse

The same workflow is not working when connecting with Node.js:

var net = require('net');
var client = net.connect(1234, 'localhost');

client.on('data', function(data) {
    console.log('data:', data.toString());
});

client.on('error', function(err) {
    console.log('error:', err.message);
});

client.write('command1\n');
client.write('command2\n');

I would expect that after running this program I would see "data: theresponse" written to the console, however, nothing is ever printed. I have also tried performing the writes inside of the "connect" callback, but I have the same results. The curious thing is that when I try this in the Node REPL...it works:

$ node
> var net = require('net')
undefined
> var client = net.connect(1234, 'localhost')
undefined
> client.on('data', function(data) { console.log('data:', data.toString()); })
{ ... }
> client.write('command1\n')
true
> client.write('command2\n')
true
> data: theresponse

Anyone have ideas about this bizarre behavior?

Thanks.

-Scott

4
  • From what you describe, is sounds like your TCP Server has problems if two or more commands arrive in one packet. Could you show the Server code? Commented May 23, 2012 at 20:20
  • I don't have access to the server code but I believe you are correct. The server is probably assuming that each "data" buffer that arrives is a single command. Thanks. Commented May 24, 2012 at 12:20
  • I must be missing something, but if it's the server's fault, why is it working in the Node.js REPL? Commented May 24, 2012 at 15:40
  • For the same reason that it worked in Netcat- it takes a little bit of time to type the next line. During that time the first command was presumably flushed to the server. Commented May 24, 2012 at 16:30

1 Answer 1

2

Without testing the code, I'm presuming it's the asynchronous nature of Node.js that's biting you. In the REPL the connection happens before you can type in another command. In your code above you are writing before you are connecting.

Change the above code to this:

var net = require('net');
var client = net.connect(1234, function(){
   client.on('data', function(data) {
     console.log('data:', data.toString());
   });

   client.on('error', function(err) {
     console.log('error:', err.message);
   });

   client.write('command1\n');
   client.write('command2\n');
});
Sign up to request clarification or add additional context in comments.

1 Comment

I tried performing the writes in a "connect" callback but experienced the same results.

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.