我正在使用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);
}
显然其他人对此似乎没有问题,但我对此有问题。有人有什么想法吗?
最佳答案
我对Quake RCON协议(protocol)的内存模糊,但是我相信特殊的 header 是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();
关于c# - 无法从RCON请求到服务器的完整响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6728032/