开发者

Parse Remote Console protocol values from a UDP payload

I am trying to throw together a quick Remote Console server in the next hour as quickly as possible, but ran into an issue.

I am using this very simp开发者_JS百科le packet protocol... http://www.codeproject.com/KB/game/gameRcon.aspx

How can I get the rest of the packet string split up properly? After those 5 bytes, it should be a space, then "rcon passwordhere", then space, then I can take the rest as a single string. I mainly just need to get the "passwordhere" part and the rest of the packet as a string after that.

public void StartServer()
{
    System.Text.ASCIIEncoding encode = new System.Text.ASCIIEncoding();

    IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 28960);
    UdpClient socket = new UdpClient(ipep);

    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);

    // Receive Packet
    byte[] data = socket.Receive(ref sender);

    if (data[0] == byte.Parse("255") &&
        data[1] == byte.Parse("255") &&
        data[2] == byte.Parse("255") &&
        data[3] == byte.Parse("255") &&
        data[4] == byte.Parse("02"))
    {
        // Check Password String

        // Execute Command
    }
}


Well, first you want to turn the remainder of your byte array into a string:

var text = encode.GetString(data, 5, data.Length - 5);

Then you can split it into the command ("rcon"), the password, and the remainder. The simplest thing to do here is to split the string on space characters, but specify that you want no more than 3 elements returned (so the final element may contain more spaces):

var segments = text.Split(new[] {' '}, 3);
// segments[0] is assumed to be "rcon"
// segments[1] is the password
// segments[2] is the remainder of the string


Looking at the "RCON" protocol it looks like it is using ASCII encoding for the string data..

string theString = System.Text.Encoding.ASCII.GetString(data.Skip(5).ToArray());

Then do whatever you like with the string, split, iterate etc...


You may want to use UTF8 instead of ASCII. This assumes there are no spaces in the first 5 bytes or in the password.

string str = System.Text.Encoding.ASCII.GetString(data);
string[] items = str.Split(' ');
string password = items[2];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜