How to add text to data relayed to tcp socket

19 views
Skip to first unread message

johanfly...@gmail.com

unread,
Jun 7, 2015, 9:34:05 AM6/7/15
to nod...@googlegroups.com
Having some problems with the code bellow, 

its a tcp client relaying data to a tcp server

Would like to add "test" to data before its resent to tcpreciver


as it is now "test" it added but it ends up on the next row in the reciving side


Reciver log:
OK   07/06/2015 06:05:33 |1;9;13/31/1;20150605;144846;3;|
OK  07/06/2015 06:05:33 |test|

Need it to look like 
OK    07/06/2015 06:05:33 |1;9;13/31/1;20150605;144846;3;test|
in the reciver end





var net = require('net');
 
var clienttcp = new net.Socket();
var tcpreciver = new net.Socket();
tcpreciver.connect(9003, '192.168.81.81', function() {
console.log('tcpreciver Connected');
});

clienttcp.connect(9002, '192.168.81.124', function() {
console.log('clienttcp Connected');
clienttcp.write('POLL\r\n');
});

var pollinterval = setInterval(function () {clienttcp.write('POLL\r\n');}, 10000);

clienttcp.on('data', function(data) {
console.log('Received: ' + data);
clienttcp.write('ACK\r\n');
  tcpreciver.write(data + 'test');   
tcpreciver.write('\r\n');
});
 
 
clienttcp.on('close', function() {
console.log('Connection clienttcp closed');
});

tcpreciver.on('close', function() {
console.log('Connection tcpreciver closed');
});






All help appriciated. 

Daniel Rinehart

unread,
Jun 7, 2015, 5:37:53 PM6/7/15
to nodejs
Check the incoming "data", it probably has a \r\n sequence terminating it that you would need to remove before appending the test and sending it along. Also be careful, based on the size of the incoming data, you may not get an entire message in a single data event.

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/24d86514-3652-4f5d-be68-a9666a27c04c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages