I'm having 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 also tried the supposedly more correct net.createConnection (arguments ...) function from the network packet with no luck.
I see on my server side that the connection to the socket is going as expected, but there is no data received by the server, so I suspect that something is wrong with the way I use the socket. write a function. Perhaps the first characters of the lines cause confusion?
Any help would be greatly appreciated.
Many thanks.
RSwan source share