C# TCP 服务器回复数据包问题

发布于 2024-10-21 14:55:58 字数 3262 浏览 2 评论 0原文

在一天的大部分时间里,我一直在用头撞我的代码,我完全被难住了。基本上,源游戏引擎有一个用于其 RCON(网络远程控制台?)的记录协议,我正在尝试重现该协议。有数百个示例,但所有示例都来自客户端(建立与游戏服务器的 RCON 的连接),因为我正在尝试实际重新创建服务器部分以回复客户端。

这是有关 RCON 协议的信息。我的代码遇到的问题是,当我收到身份验证请求时,一切都很好。当我尝试回复它并确定连接时,连接失败。所以我在回复时做错了什么,但不确定是什么。

http://developer.valvesoftware.com/wiki/Source_RCON_Protocol

private void ReadClientPacket(object client)
{
    TcpClient tcpClient = (TcpClient)client;
    NetworkStream clientStream = tcpClient.GetStream();

    while (true)
    {
        try
        {
            int packetsize;

            // Create a new Packet Object and fill out the data from the incoming TCP Packets
            RCONPacket packet = new RCONPacket();

            using (BinaryReader reader = new BinaryReader(clientStream))
            {
                // First Int32 is Packet Size
                packetsize = reader.ReadInt32();

                packet.RequestId = reader.ReadInt32();
                packet.RconDataReceived = (RCONPacket.RCONDATA_rec)reader.ReadInt32();

                Console.WriteLine("Packet Size: {0} RequestID: {1} ServerData: {2}", packetsize, packet.RequestId, packet.RconDataReceived);

                // Read first and second String in the Packet (UTF8 Null Terminated)
                packet.String1 = ReadBytesString(reader);
                packet.String2 = ReadBytesString(reader);

                Console.WriteLine("String1: {0} String2: {1}", packet.String1, packet.String2);

                switch (packet.RconDataReceived)
                {
                    case RCONPacket.RCONDATA_rec.SERVERDATA_AUTH:
                    {
                        ReplyAuthRequest(packet.RequestId, tcpClient);
                        break;
                    }
                    case RCONPacket.RCONDATA_rec.SERVERDATA_EXECCOMMAND:
                    {
                        //ReplyExecCommand(packet.RequestId, tcpClient);
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }

            break;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            break;
        }
    }

    tcpClient.Close();
}

private void ReplyAuthRequest(int RequestID, TcpClient client)
{
    Console.WriteLine("Replying to Auth Request");

    // Authentication Reply
    using (NetworkStream clientStream = client.GetStream())
    using (MemoryStream stream = new MemoryStream())
    using (BinaryWriter writer = new BinaryWriter(stream))
    {
        writer.Write((int)10); // Packet Size
        writer.Write(RequestID); // Mirror RequestID if Authenticated, -1 if Failed
        writer.Write((int)RCONPacket.RCONDATA_sent.SERVERDATA_AUTH_RESPONSE);
        writer.Write(ConvertStringToByteArray("" + char.MinValue));
        writer.Write(ConvertStringToByteArray("" + char.MinValue));
        byte[] buffer = stream.ToArray();
        Console.WriteLine("size of full auth response packet is {0}", buffer.Length);

        clientStream.Write(buffer, 0, buffer.Length);
        clientStream.Flush();
    }
}

I have been banging my head against my code for the better part of the day, and I am completely stumped. Basically, the source game engine has a documented protocol for its RCON (Remote Console Over Network?) which I am trying to reproduce. There are hundreds of examples, but all of them are from the client side (establishing a connection to the game server's RCON) where as I am trying to actually re-create the server portion to reply to clients.

Here is the information on the RCON Protocol. The problem I am having with the code is, when I receive the Authentication request everything is fine. When I attempt to reply to it and okay the connection, the connection fails. So I am doing something wrong when replying but not sure what.

http://developer.valvesoftware.com/wiki/Source_RCON_Protocol

private void ReadClientPacket(object client)
{
    TcpClient tcpClient = (TcpClient)client;
    NetworkStream clientStream = tcpClient.GetStream();

    while (true)
    {
        try
        {
            int packetsize;

            // Create a new Packet Object and fill out the data from the incoming TCP Packets
            RCONPacket packet = new RCONPacket();

            using (BinaryReader reader = new BinaryReader(clientStream))
            {
                // First Int32 is Packet Size
                packetsize = reader.ReadInt32();

                packet.RequestId = reader.ReadInt32();
                packet.RconDataReceived = (RCONPacket.RCONDATA_rec)reader.ReadInt32();

                Console.WriteLine("Packet Size: {0} RequestID: {1} ServerData: {2}", packetsize, packet.RequestId, packet.RconDataReceived);

                // Read first and second String in the Packet (UTF8 Null Terminated)
                packet.String1 = ReadBytesString(reader);
                packet.String2 = ReadBytesString(reader);

                Console.WriteLine("String1: {0} String2: {1}", packet.String1, packet.String2);

                switch (packet.RconDataReceived)
                {
                    case RCONPacket.RCONDATA_rec.SERVERDATA_AUTH:
                    {
                        ReplyAuthRequest(packet.RequestId, tcpClient);
                        break;
                    }
                    case RCONPacket.RCONDATA_rec.SERVERDATA_EXECCOMMAND:
                    {
                        //ReplyExecCommand(packet.RequestId, tcpClient);
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }

            break;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            break;
        }
    }

    tcpClient.Close();
}

private void ReplyAuthRequest(int RequestID, TcpClient client)
{
    Console.WriteLine("Replying to Auth Request");

    // Authentication Reply
    using (NetworkStream clientStream = client.GetStream())
    using (MemoryStream stream = new MemoryStream())
    using (BinaryWriter writer = new BinaryWriter(stream))
    {
        writer.Write((int)10); // Packet Size
        writer.Write(RequestID); // Mirror RequestID if Authenticated, -1 if Failed
        writer.Write((int)RCONPacket.RCONDATA_sent.SERVERDATA_AUTH_RESPONSE);
        writer.Write(ConvertStringToByteArray("" + char.MinValue));
        writer.Write(ConvertStringToByteArray("" + char.MinValue));
        byte[] buffer = stream.ToArray();
        Console.WriteLine("size of full auth response packet is {0}", buffer.Length);

        clientStream.Write(buffer, 0, buffer.Length);
        clientStream.Flush();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

可爱暴击 2024-10-28 14:55:58

您是否使用过Wireshark

当您尝试复制/逆向工程现有协议时,此工具至关重要。只需使用已知工作的客户端进行正常身份验证并保存日志即可。然后使用您自己的代码并尝试查看线路上发送的位与日志中的位有何不同。

有时,仅通过查看代码就很难看到一些事情,例如在错误的位置插入了“\n”,或者在整个消息后面添加了一行

Have you been using Wireshark?

This tool is essential when you try to copy/reverse-engineer existing protocols. Just do a normal authentication with a known-working client and save the log. Then use your own code and try to see where the bits sent on the wire are different from those in the log.

Sometimes its pretty difficult things to see just by looking at the code, like a '\n' getting inserted at the wrong point, or an extra line after the whole message

变身佩奇 2024-10-28 14:55:58

不久前我

在您的 Reply 函数中遇到了类似的问题:

using (NetworkStream clientStream = client.GetStream())
(...)

clientStream 的处置可能是罪魁祸首。在我的例子中,流的处理导致连接终止,GetStream() 不返回流的新实例,它返回 TCPClient 拥有的流。看看是否有帮助。

I've had a similar problem a while ago

In your Reply function :

using (NetworkStream clientStream = client.GetStream())
(...)

The disposal of the clientStream could be the culprit. In my case the disposal of the stream caused the connection termination, GetStream() doesn't return a new instance of a stream, it returns the Stream that is owned by the TCPClient. See if that helps.

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