5
我需要从一个node.js TCP服务器向一些TCP客户端发送一个JSON字符串。如何通过node.js来发送通过TCP发送的消息?
为了从客户端的套接字/流读取消息,我需要做一些消息成帧。一种方法是将消息的长度作为数组前缀,然后将其转换为客户端消息的缓冲区大小。
我该如何在服务器上的node.js/javascript中执行类似操作,然后使用.NET客户端在客户端读取它?
鉴于此客户端代码,我将如何使用javascript/node在服务器端正确地构建消息?
TcpClient client = new TcpClient(server, port);
var netStream = client.GetStream();
// read the length of the message from the first 4 bytes
byte[] b = new byte[4];
netStream.Read(b, 0, b.Length);
int messageLength = BitConverter.ToInt32(b, 0);
// knowing the length, read the rest of the message
byte[] buffer = new byte[messageLength];
netStream.Read(buffer, b.Length, buffer.Length);
var message = System.Text.Encoding.UTF8.GetString(buffer);
Buffer类是我所需要的。我想我应该先有RTFM。谢谢@Andrey。 –
注意0.5被认为是不稳定的,并且在0.4.9缓冲区中没有int/float序列化器 –