Not logged in

Please login with username, password and session length or register.

Advanced search  

Author Topic: RCON protocol  (Read 3151 times)


  • Engineer
  • **
  • Offline
  • Posts: 51
RCON protocol
« on: August 05, 2014, 07:55:32 am »

Recently I was looking at a way to create something that used the Remote Connection protocol that you see used to administer different source type servers.
Valve have a wiki here on it

The protocol is fairly common across multiple game servers running source as a base.

In the above link, there is a C++ example of how to construct the packet. I tried using it, but the example that the user submitted is deeply flawed.

Code: [Select]
unsigned char[] RCON_Command(std::string Command, int ServerData)
unsigned char Packet[static_cast<unsigned char>((13 + Command.length())) + 1];
Packet[0] = Command.length() + 9; //Packet Size (Integer)
Packet[4] = 0; //Request Id (Integer)
for (int X = 0; X < Command.length(); X++)
Packet[12 + X] = System::Text::Encoding::Default->GetBytes(Command[X])[0];
return Packet;

I approached Stealth Eye for some advice and help, and he kindly pointed out the following.

Code: [Select]
1. It seems to be a C++/CLI application. i.e. a C++ application using
the .NET framework (System::Text::Encoding::* is a .NET API). You need
to add the /clr compiler flag to compile it, or rewrite it not to use
that function. I don't even think it is correctly used, so rewriting
makes sense. ;)
2. The declaration uses "unsigned char*" as return type and the
implementation uses "unsigned char[]". They should match, and I believe
VC++ does not like "unsigned char[]".
3. The types are incorrect. It writes the characters as bytes instead of
as integers.
4. It uses a dynamic size stack allocated array, and returns it. Afaik,
VC++ only supports stack allocated arrays of fixed size, and I don't
think returning one is valid in any compiler.
I don't think this worked for whoever submitted the code to that site. I
rewrote it to:
std::string RCON_Command(std::string command, int serverData)
const unsigned int requestId = 0;
std::string packet;
packet.resize(12 + command.length() + 1);
(unsigned int&)packet[0] = packet.size() - 4;
(unsigned int&)packet[4] = requestId;
(unsigned int&)packet[8] = serverData;
std::copy(command.begin(), command.end(), &packet[12]);
packet[12 + command.length()] = '\0';
return packet;

Pretty cool of him to help. Thank you very much Seye. :-)



  • Founder
  • Staff
  • Medium Tank driver
  • ******
  • Offline
  • Posts: 939
Re: RCON protocol
« Reply #1 on: August 05, 2014, 07:28:18 pm »

Heh, you're welcome. Thanks for posting the solution. ;)

December 06, 2021, 08:42:20 am