node.js缓冲tcp数据和分隔符

时间:2012-07-20 19:14:21

标签: node.js tcp

我正在尝试编写一个快速而脏的tcp服务器,并且遇到分隔符问题。每this question我正在缓冲传入数据并查找分隔符(在本例中为'\ r \ n'。但是,当我telnet并发送消息时

foo\r\nbar

下面的服务器无法识别中间的分隔符,但确实在末尾看到了\ r \ n-我的印象是telnet只发送了一个\ n。当我通过ruby脚本发送消息时,即使消息中存在\ r \ n,也不会在任何地方识别分隔符。

我需要注意一些js字符串处理行为吗?

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:
    // https://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);

2 个答案:

答案 0 :(得分:0)

实际上,您发布的代码没有任何问题,它可以正常运行。

当你使用telnet发送字符串“\ r \ n”时,它只是将它作为文字传递给我,所以我真的不明白那个例子(也许你粘贴了它?)。

此外,我在console.log中注释了processTCP来电。

使用此代码测试它(您可以直接从node.js的控制台运行它):

require('net').connect(6969, '127.0.0.1').write('segment1\r\nsegment2');

我在服务器端获得的结果:

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

显然,您的代码运行正常。

答案 1 :(得分:0)

如果您的服务器收到如下消息,则会出现问题:

ECHO我爱你\ r

\ n

分隔符收到两个包。