10

I am having some trouble writing 2 messages to a TCP socket using node.js' net package.

The code:

var net = require('net');


var HOST = '20.100.2.62';
var PORT = '5555';

var socket = new net.Socket();

socket.connect (PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
// Write a message to the socket as soon as the client is connected, the server will   receive it as message from the client 
  socket.write('@!>');       
  socket.write('RIG,test,test,3.1');

});



// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
socket.on('data', function(data) {
  console.log('DATA: ' + data);
  // Close the client socket completely
  //    client.destroy();
});

socket.on('error', function(exception){
  console.log('Exception:');
  console.log(exception);
});


socket.on('drain', function() {
  console.log("drain!");
});

socket.on('timeout', function() {
  console.log("timeout!");
});

// Add a 'close' event handler for the client socket
socket.on('close', function() {
   console.log('Connection closed');
});

I've also tried the supposedly more correct net.createConnection(arguments...) function from the net package with no luck.

I can see on my server side that the connection to the socket happens just as expected but there is no data received by the server which is why I'm suspecting that something is wrong with the way I'm using the socket.write function. Perhaps the first strings characters are causing confusion?

Any help would be greatly appreciated.

Many thanks.

5
  • I tried your code, using nc -l 5555 as my server, and it seems to work fine under Node 0.6.10. Commented Feb 17, 2012 at 13:39
  • 1
    Thanks so much for trying! Problem is I can get it to work (using Node 0.5.11-pre though) with nc, or with a node.js server that I wrote myself ... its just this particular server (that was written in java) that I cannot connect to. I can use telnet to connect and send message/data perfectly fine, but not when using Socket.write(). As I say when I do a tail on the servers log file I can clearly see I'm connected, but whatever I try to write to the tcp socket (with above code) it just isn't showing up. Thanks for your reply, I appreciate it. Commented Feb 17, 2012 at 14:12
  • In that case, I'm guessing you're not formatting your data properly. You might want to try to send line-breaks, for instance (telnet would do this). Try socket.write('@!>\n');. Commented Feb 17, 2012 at 14:14
  • Thanks man! Even though I probably should've known about the line-breaks, you made my day. Cheers Commented Feb 17, 2012 at 14:33
  • 3
    Cool - I have provided the same information as a real answer, for future reference and for you to provide me with sweet points - please accept my answer. Commented Feb 17, 2012 at 14:53

1 Answer 1

25

It depends on what server you are speaking to, obviously, but you should probably delimit your data with newlines \n:

socket.write('@!>\n');       
socket.write('RIG,test,test,3.1\n');

For some servers, you might need \r\n.

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.