节点js缓冲区添加NULL终止消息

node js Buffer add NULL terminated message

首先尝试使用节点 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);
});

简单连接缓冲区

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