nodejs Buffer()的问题

时间:2013-06-18 08:27:11

标签: node.js buffer

使用Buffer(),我将数据包标头形成为100字节

根据标题的形成规则,我必须指明以下内容:

Offset  Length(bytes)   Type    Description
0       4               Int     The length of the message (no header)
4       4               Int     Time to create the query (the number of seconds since January 1, 1970 GMT)
8       4               Int     The message ID
12      32                      Reserved (filled with null byte)
44      2               Int     The client ID
46      1                       1st byte of message flags
47      1                       2nd byte of message flags
48      4               Int     The identifier of the symmetric key
52      48                      Reserved (filled with null byte)

这是我的代码:

var query="<?xml version=\"1.0\" encoding=\"utf-8\"?><sirena><query><get_currency_rates><curr1>RUB</curr1><curr2>USD</curr2><owner>IATA</owner></get_currency_rates></query></sirena>";

        var buf=new Buffer(100);
        var query1=new Buffer(query);
        console.log(query1.length);
        buf.writeInt32BE(query1.length, 0, true);//Длина текста сообщения (без заголовка)

        var foo = new Date;
        var unixtime_ms = foo.getTime();
        var unixtime = parseInt(unixtime_ms / 1000);
        console.log(unixtime);
        buf.writeInt32BE(unixtime, 4, true);//Время создания запроса (кол-во секунд с 1 января 1970 GMT)

        buf.writeInt32BE(1, 8, true);//id сообщения. потом нужно автоинкрементить

        for(var i=12; i<44;i++){//Зарезервировано (заполнено нулевым байтом)
            buf.writeInt8(0, i, true);
        }


        buf.writeInt16BE(5985, 44, true);//Идентификатор клиента

        buf.writeInt8(0, 46, true);//1-й байт флагов сообщения - обязательно ли это??
        buf.writeInt8(0, 47, true);//2-й байт флагов сообщения - обязательно ли это??

        buf.writeInt32BE(0, 48, true);//Идентификатор симметричного ключа - обязательно ли это??


        for(var i=52; i<100;i++){//Зарезервировано (заполнено нулевым байтом)
            buf.writeInt8(0, i, true);
        }

        var packet=buf.toString();//+query;

这是给出buf.inspect()

的字节顺序
<Buffer 00 00 00 a6 51 c0 15 db 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 17 61 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00>

但它传递给套接字

0000 00ef bfbd 51ef bfbd 15ef bfbd 0000
0001 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 1761 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 

发生了什么事?

1 个答案:

答案 0 :(得分:0)

您在转化字符串&lt; - &gt;缓冲区

中使用了不同的编码
//...
console.log(buf);
var packet=buf.toString('binary');//+query;
console.log(Buffer(packet, 'binary'));
//...

- &GT;

<Buffer 00 00 00 a6 51 c0 27 d6 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00 00 00 00 00 17 61 00 00 00 00 00 ...>
<Buffer 00 00 00 a6 51 c0 27 d6 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 17 61 00 00 00 00 00 ...>

//...
console.log(buf);
var packet=buf.toString();//+query; // looks like utf8 here
console.log(Buffer(packet, 'binary')); // and binary here (even without explicit 'binary')
//...

- &GT;

<Buffer 00 00 00 a6 51 c0 27 d6 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00 00 00 00 00 17 61 00 00 00 00 00 ...>
<Buffer 00 00 00 fd 51 fd 28 28 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 17 61 00 00 00 00 00 ...>