I am using C# to query a Call of Duty 4 rcon to get status for the players, it works fine but it does not appear to receive a response of more than 1303 characters.
public string sendCommand(string rconCommand, string ga开发者_开发技巧meServerIP,
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);
}
Apparently other people seem not have a problem with this but I am having problems with it. Does any one have any ideas?
My memory of the Quake RCON protocol is hazy, but I believe the special header is 5 bytes, 0xFF 0xFF 0xFF 0xFF 0x02
. If that is not the case for COD4, then ignore this advice.
Additionally, you can't rely on the data being received all in one call to Socket.Receive
. And my experience with Quake RCON is isn't always the best in returning, "as expected".
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();
精彩评论