【发布时间】:2013-07-01 00:21:47
【问题描述】:
我正在尝试编写一个节点服务器,我需要在其中向 c# 客户端发送一个 32 位整数(作为标头)。
我不太确定该怎么做,因为位移运算符让我很困惑。我认为我的 c# 客户端期望这些整数采用小端格式(我不确定,我这么说是因为 NetworkStream IsLittleEndian 属性为真)。
假设我在 javascript 中有一个类似于
的变量var packetToDeliverInBytes = GetByteArrayOfSomeData();
//get the integer we need to turn into 4 bytes
var sizeOfPacket = packetToDeliver.length;
//this is what I don't know how to do
var bytes = ConvertNumberTo4Bytes(sizeOfPacket)
//then somehow do an operation that combines these two byte arrays together
//(bytes and packetToDeliverInBytes in this example)
//so the resulting byte array would be (packetToLiver.length + 4) bytes in size
//then send the bytes away to the client
socket.write(myByteArray);
如何编写 ConvertNumberTo4Bytes() 函数?
奖金
如何将这 2 个字节数组合并为一个,以便在一个 socket.write 调用中发送它们
【问题讨论】:
-
看看这是否有帮助:nodejs.org/api/…
标签: c# javascript node.js network-programming