如何正确写入UDP数据包

发布于 2024-10-21 17:39:05 字数 523 浏览 1 评论 0原文

我正在尝试从我不久前编写的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒路情人 2024-10-28 17:39:05

BinaryWriter 可能会工作,尽管字符串是用前面的 7 位编码长度编写的,我怀疑客户端将无法处理。您可能必须将字符串转换为字节,然后添加一个长度字或以 0 结尾。

无需手动将数字转换为字节。如果您想将一个long 写为byte,只需将其转换即可。也就是说,如果您的 BinaryWriterbw,那么您可以编写 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 a byte, just cast it. That is, if your BinaryWriter is bw, then you can write bw.Write((byte)longval);. To write -1 as a long: bw.Write((long)(-1)).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文