2011-06-23 58 views
1

我试图从C#NET应用程序实现此协议(http://developer.valvesoftware.com/wiki/Source_RCON_Protocol)。适用于我正在实施的代码的部分位于标题“接收”下。但是,我并不积极,在构造数据包时我的字节大小是正确的。数据包的字节大小

这里是我的功能来构建一个包......

private static byte[] ConstructPacket(int request_id, int cmdtype, string cmd) 
    { 
     MemoryStream stream = new MemoryStream(); 
     using (BinaryWriter writer = new BinaryWriter(stream)) 
     { 
      byte[] cmdBytes = ConvertStringToByteArray(cmd); 
      int packetSize = 12 + cmdBytes.Length; 

      // Packet Contents 
      writer.Write((int)packetSize); // Byte size of Packet not including This 
      writer.Write((int)request_id); // 4 Bytes 
      writer.Write((int)cmdtype);  // 4 Bytes 
      writer.Write(cmdBytes);   // 8 Bytes ?? 

      // NULL String 1 
      writer.Write((byte)0x00); 
      writer.Write((byte)0x00); 

      // NULL String 2 
      writer.Write((byte)0x00); 
      writer.Write((byte)0x00); 

      // Memory Stream to Byte Array 
      byte[] buffer = stream.ToArray(); 

      return buffer; 
     } 
    } 

根据该协议规范,分组大小是分组中不包括本身的字节大小。

第2(INT)将使8个字节...... 的“cmdBytes”,在这种情况下paticular是“testpass”将是8个字节。我相信 在最后2个空分隔字符串(如果我将这些设置正确)将是4个字节。

所以通过我的计算,数据包应该是20个字节大,但它似乎没有正常工作。我正在考虑这些值应该都是正确的,我是否正确地为C#.NET设置NULL delmited字符串?

回答

0

你写了两个太多的零。在例子中很容易看到,它们都以两个零而不是四个结尾。

+0

你是什么意思?喜欢这个? // NULL String 1 writer.Write((byte)0); writer.Write((byte)0); –

0

您应该在最后的writer.Write()之后拨打writer.Flush()。否则,在完成将所有内容写入流之前,可能会冒着处置作者的风险。