I'm trying to write a quick and dirty tcp server and have trouble with delimiters. Per this question I'm buffering incoming data and looking for delimiters (in this case '\r\n'. However, when I telnet in and send a message
foo\r\nbar
The below server doesn't recognize the delimiter in the middle, but does see a \r\n at the end- I was under the impression telnet send a \n only. When I send messages via a ruby script, no delimiter is recognized anywhere, even if \r\n is present in the message.
Is there some js string handling behavior that I need to be awared of?
var net = require("net");
var http = require('http');
var HOST = '127.0.0.1';
var PORT = 6969;
var TCP_DELIMITER = '\r\n';
var TCP_BUFFER_SIZE = Math.pow(2,16);
net.createServer(function(sock) {
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);
// To buffer tcp data see:
// http://stackoverflow.com/questions/7034537/nodejs-what-is-the-proper-way-to-handling-tcp-socket-streams-which-delimiter
buf = new Buffer(TCP_BUFFER_SIZE); //new buffer with size 2^16
processTCP = function(msg) {
// process messages
console.log("processTCP: "+msg);
}
// socket handlers
sock.on('data', function(data) {
// look for separator '\r\n'
console.log("data='"+data+"'");
data = data.toString('utf-8');
if(data.indexOf(TCP_DELIMITER) == -1) {
console.log("1 PART MSG, INCOMING");
buf.write(data.toString()); // write data to buffer
} else {
parts = data.toString().split(TCP_DELIMITER);
console.log("Parts: "+parts);
if (parts.length == 2) {
console.log("2 PART MSG, INCOMING");
msg = buf.toString() + parts[0]; // and do something with message
processTCP(msg);
buf = (new Buffer(TCP_BUFFER_SIZE)).write(parts[1]); // write new, incomplete data to buffer
} else {
console.log(parts.length+" PART MSG, INCOMING");
msg = buf.toString() + parts[0];
processTCP(msg);
for (var i = 1; i <= parts.length -1; i++) {
if (i !== parts.length-1) {
msg = parts[i];
processTCP(msg);
} else {
buf.write(parts[i]);
}
}
}
}
console.log('DATA ' + sock.remoteAddress + ': ' + data);
// Write the data back to the socket, the client will receive it as data from the server
sock.write('You said "' + data + '"');
});
sock.on('close', function(data) {
console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
});
}).listen(PORT, HOST);
console.log('Server listening on ' + HOST +':'+ PORT);
Actually, there's nothing wrong with the code you posted, it does work properly.
When you send the string "\r\n" using telnet, it would simply pass it as a literal so I didn't really understand that example (perhaps you pasted it?).
Also, I commented out the console.log
call in processTCP
.
Test it using this code (you can just run it directly from node.js
's console):
require('net').connect(6969, '127.0.0.1').write('segment1\r\nsegment2');
The results I got on the server side:
Server listening on 127.0.0.1:6969
CONNECTED: 127.0.0.1:34960
Parts: segment1,segment2
2 PART MSG, INCOMING
DATA 127.0.0.1: segment1
segment2
CLOSED: undefined undefined
Clearly, your code works just fine.
There is a problem if your server receive the message like below:
ECHO I love you\r
\n
The delimiter is received in two package.