node js Buffer添加NULL终止消息

时间:2018-02-05 13:59:52

标签: javascript node.js tcp buffer

首先尝试使用节点js和Tcp套接字。

对于有效请求,我需要将带有XML的消息发送到需要以null(0x00)终止的服务器。

到目前为止,我想出了下面的代码来发送消息,但我不知道如何添加NULL终止符。

const client = new net.Socket();

client.connect(port, address, () => {
 attachments.forEach((attachment) => {
    const request = setValueToXml(attachment);
    const buffer = new Buffer(request);
    client.write(buffer); // <-- What do I need to add here?
  });
});
client.on('data', (resp) => {
  console.log('You have mail', resp);
});

1 个答案:

答案 0 :(得分:1)

简单地连接缓冲区

 var bufferNull = new Buffer([0x00])
 buffer = Buffer.concat([buffer, bufferNull]);
 client.write(buffer);