如何正确写入UDP数据包
我正在尝试从我不久前编写的 C++ 程序中重写一些代码,但我不确定是否/如何正确写入字节数组,或者我是否应该使用其他东西。我尝试更改为 C# .NET 的代码如下。
unsigned char pData[1400];
bf_write g_ReplyInfo("SVC_ReplyInfo", &pData, 1400);
void PlayerManager::BuildReplyInfo()
{
// Delete the old packet
g_ReplyInfo.Reset();
g_ReplyInfo.WriteLong(-1);
g_ReplyInfo.WriteByte(73);
g_ReplyInfo.WriteByte(g_ProtocolVersion.GetInt());
g_ReplyInfo.WriteString(iserver->GetName());
g_ReplyInfo.WriteString(iserver->GetMapName());
}
I am trying to rewrite some of my code from a C++ program I wrote a while ago, but I am not sure if/how I can write to a byte array properly, or if I should be using something else. The code I am trying to change to C# .NET is below.
unsigned char pData[1400];
bf_write g_ReplyInfo("SVC_ReplyInfo", &pData, 1400);
void PlayerManager::BuildReplyInfo()
{
// Delete the old packet
g_ReplyInfo.Reset();
g_ReplyInfo.WriteLong(-1);
g_ReplyInfo.WriteByte(73);
g_ReplyInfo.WriteByte(g_ProtocolVersion.GetInt());
g_ReplyInfo.WriteString(iserver->GetName());
g_ReplyInfo.WriteString(iserver->GetMapName());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
BinaryWriter
可能会工作,尽管字符串是用前面的 7 位编码长度编写的,我怀疑客户端将无法处理。您可能必须将字符串转换为字节,然后添加一个长度字或以 0 结尾。无需手动将数字转换为字节。如果您想将一个
long
写为byte
,只需将其转换即可。也就是说,如果您的BinaryWriter
是bw
,那么您可以编写bw.Write((byte)longval);
。要将-1
写为 long:bw.Write((long)(-1))
。BinaryWriter
might work, although strings are written with a preceding 7-bit encoded length, which I suspect the client won't be able to handle. You'll probably have to convert strings to bytes and then either add a length word or 0-terminate it.No need to manually convert numbers to bytes. If you have a
long
that you want to write as abyte
, just cast it. That is, if yourBinaryWriter
isbw
, then you can writebw.Write((byte)longval);
. To write-1
as a long:bw.Write((long)(-1))
.