MWF2 RCON TOOL使用indyUDP与delphi通信

时间:2016-01-09 11:07:33

标签: delphi indy

Modern Warefare 2(MWF2):是一款视频游戏。 RCON TOOL:是一种用于使用UDP向游戏服务器发送命令的工具。

我正在尝试使用indy向服务器发送命令,它很容易使用idUDPclient发送一些字符串,但我的问题是 我应该以这种格式发送它:

ÿÿÿÿrcon  "1234" kick cheater101

where :-
password quoted: "1234"
and command is : kick cheater101 

像这样的字节

   FFFFFFFF72636F6E2020223132333422206B69636B2063686561746572313031

请注意,发送的任何内容都必须以FFFFFFFF开头 以及它应该如何看待Wireshark ..  enter image description here

问题是我不能像上面那样发送..我只是像字符串一样发送..

我需要用indyUDP制作它,因为我打算在android上测试它。

这是我正在尝试的代码:

  function rcon(const IP: String; Port: TIdPort; const Pass, Command: String): String;
  var
  Query: TIdBytes;
  Buffer, Data:
  TIdBytes;
  Len: Integer;
  begin
  SetLength(Query, 4);
  Query[0] := $FF;
  Query[1] := $FF;
  Query[2] := $FF;
  Query[3] := $FF;
  AppendString(Query, 'rcon "' + Pass + '" ' + Command);
  SetLength(Data, 0);
  with TIdUDPClient.Create do try ReceiveTimeout := 2000;
  SendBuffer(IP, Port, Query);
  repeat SetLength(Buffer, 10000);
  Len := ReceiveBuffer(Buffer);
  if Len < 1 then Break;
  SetLength(Buffer, Len);
  AppendBytes(Data, Buffer);
  until False; finally Free;
  end; // preprocess Data as needed... Result := BytesToString(Data);
 end

用法!

 rcon('10.0.0.4', 28961, '1234', 'kick cheater101');

Wireshark的:  enter image description here

2 个答案:

答案 0 :(得分:2)

您遇到 DISPLAY 问题,而不是 DATA 问题。在Wireshark屏幕截图中,您按原样查看原始字节,而不是那些相同字节的十六进制格式化表示。 Wireshark可以向你们展示。

您发送的实际字节数很好 - 除了您在rcon和引用密码之间缺少第二个空格字符。

function rcon(const IP: String; Port: TIdPort; const Pass, Command: String): String;
var
  Query: TIdBytes;
  Buffer, Data: TIdBytes;
  Len: Integer;
begin
  SetLength(Query, 4);
  FillBytes(Query, 4, $FF);
  AppendString(Query, 'rcon  "' + Pass + '" ' + Command);

  SetLength(Data, 0);
  with TIdUDPClient.Create do
  try
    SendBuffer(IP, Port, Query);
    ReceiveTimeout := 2000;
    SetLength(Buffer, 10000);
    repeat
      Len := ReceiveBuffer(Buffer);
      if Len < 1 then Break;
      AppendBytes(Data, Buffer, 0, Len);
    until False;
  finally
    Free;
  end;
  // preprocess Data as needed...
  Result := BytesToString(Data);
end;

此外,在Wireshark屏幕截图中,它清楚地显示了为每个命令发送的70个字节,其中命令本身为32个字节,其余为38个字节的填充。如果服务器需要填充,则需要将其添加到外发Query

function rcon(const IP: String; Port: TIdPort; const Pass, Command: String): String;
var
  Query: TIdBytes;
  Buffer, Data: TIdBytes;
  Len: Integer;
begin
  SetLength(Query, 4);
  FillBytes(Query, 4, $FF);
  AppendString(Query, 'rcon  "' + Pass + '" ' + Command);
  if (Length(Query) < 70) then
    ExpandBytes(Query, Length(Query), 70-Length(Query));

  SetLength(Data, 0);
  with TIdUDPClient.Create do
  try
    SendBuffer(IP, Port, Query);
    ReceiveTimeout := 2000;
    SetLength(Buffer, 10000);
    repeat
      Len := ReceiveBuffer(Buffer);
      if Len < 1 then Break;
      AppendBytes(Data, Buffer, 0, Len);
    until False;
  finally
    Free;
  end;
  // preprocess Data as needed...
  Result := BytesToString(Data);
end;

答案 1 :(得分:1)

这个工作,它将返回正确的字节数组(反正作为AnsiString)!我使用format来组合输入,并在FFFFFFFF之前添加

Function MakeKickCommand(pass, user: LPCSTR): AnsiString; //is wide strings okay
var
  Temp  : AnsiString;
  len, I: integer;
  a: integer;
Begin
  Temp:= AnsiString( format('rcon  "%s" kick %s', [pass, user]) );
  len := length(Temp);

  //set length of the result
  SetLength(Result, 4+len);

  //add FF FF FF FF
  FillChar (Result[1], 4, $FF);
  //copy the formatted string
  Move(Temp[1], Result[5], len);
End;