没有得到rcon请求到服务器的完整响应

时间:2011-07-18 01:54:02

标签: c# .net sockets

我正在使用C#来查询“使命召唤4”rcon以获取玩家的状态,它工作正常,但似乎没有收到超过1303个字符的响应。

public string sendCommand(string rconCommand, string gameServerIP,
    string password, int gameServerPort)
{
    Socket client = new Socket(AddressFamily.InterNetwork,
                               SocketType.Dgram,
                               ProtocolType.Udp);
    client.Connect(IPAddress.Parse(gameServerIP), gameServerPort);

    string command;
    command = "rcon " + password + " " + rconCommand;
    byte[] bufferTemp = Encoding.ASCII.GetBytes(command);
    byte[] bufferSend = new byte[bufferTemp.Length + 4];

    bufferSend[0] = byte.Parse("255");
    bufferSend[1] = byte.Parse("255");
    bufferSend[2] = byte.Parse("255");
    bufferSend[3] = byte.Parse("255");
    int j = 4;

    for (int i = 0; i < bufferTemp.Length; i++)
    {
        bufferSend[j++] = bufferTemp[i];
    }

    IPEndPoint RemoteIpEndPoint
        = new IPEndPoint(IPAddress.Parse(gameServerIP), 0);
    client.Send(bufferSend, SocketFlags.None);
    byte[] bufferRec = new byte[64999];
    client.Receive(bufferRec);
    return Encoding.ASCII.GetString(bufferRec);
}

显然其他人似乎没有这个问题,但我遇到了问题。有没有人有任何想法?

1 个答案:

答案 0 :(得分:0)

我对Quake RCON协议的记忆很模糊,但我相信特殊标头是5个字节,0xFF 0xFF 0xFF 0xFF 0x02。如果不是COD4的情况,那么忽略这个建议。

此外,您不能完全依赖于Socket.Receive的一次通话中收到的数据。我对Quake RCON的体验并不总是最好的回归,“正如预期的那样”。

byte[] bufferSend = new byte[bufferTemp.Length + 5 ];
bufferSend[0] = 0xFF;
bufferSend[1] = 0xFF;
bufferSend[2] = 0xFF;
bufferSend[3] = 0xFF;
bufferSend[4] = 0x02;
Buffer.BlockCopy(bufferTemp, 0, bufferSend, 5, bufferTemp.Length);
...

StringBuilder response = new StringBuilder();
byte[] bufferRecv = new byte[65536];
do
{
    // loop on receiving the bytes
    int bytesReceived = client.Receive(bufferRecv);

    // only decode the bytes received
    response.Append(Encoding.ASCII.GetString(bufferRecv, 0, bytesReceived));
} while (client.Available > 0);

return response.ToString();