0

I'm trying to implement Query Mechanism in Samp. I writed code, but it does not working properly. I was trying to get all information from packet 'i'. I was using this website to got some informations about my problem. Unfortunately I do not understand it enough. https://wiki.sa-mp.com/wiki/Query/Request#Recieving_the_packets

Main problem its that, I can't get more players than 126 from the maxplayers. I realized that is the max index of ASCII printable characters.

unit Unit6;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdBaseComponent,
  IdComponent, IdUDPBase, IdUDPClient, IdIPWatch;

type
  TForm6 = class(TForm)
    StrList: TMemo;
    UDPClient: TIdUDPClient;
    Button1: TButton;
    IdIPWatch1: TIdIPWatch;
    hostname: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form6: TForm6;

const
ip: string = '109.230.228.151';
port: integer = 7777;

implementation

{$R *.dfm}

procedure TForm6.Button1Click(Sender: TObject);
var
Players,MaxPlayers,receivedstring,kopreceivedstring,kopreceivedstring2,Buffer:string;
stringlen,stringlenfromtext:integer;
begin
  StrList.Lines.Text:= ip;
  StrList.Lines.Text:=StringReplace(StrList.Lines.Text, '.', #13, [rfReplaceAll]);
  Buffer := 'SAMP' + Chr(StrToIntDef(StrList.Lines[0], 100)) + Chr(StrToIntDef(StrList.Lines[1], 100)) + Chr(StrToIntDef(StrList.Lines[2], 100)) + Chr(StrToIntDef(StrList.Lines[3], 100));
  Buffer := Buffer + Chr(port and 255) + Chr((port shr 8) and 255)+ 'i';

    UDPClient.Host:=IdIPWatch1.LocalIP;
    UDPClient:= TIdUDPClient.Create(self);
    UDPClient.Active := True;
    UDPClient.Send(ip,port,Buffer);
    UDPClient.ReceiveTimeout := 1500;
    receivedstring:=UDPClient.ReceiveString();
      if (receivedstring<>'') then
      begin
      kopreceivedstring:=receivedstring;
      kopreceivedstring2:=kopreceivedstring;
      Delete(receivedstring, 1, 1);
      Delete(kopreceivedstring,1,1);
      Delete(kopreceivedstring2,1,1);
        Players:=receivedstring;
        MaxPlayers:=receivedstring;
      label3.Caption:= inttostr(ord(Players[12]))+' / '+inttostr(ord(MaxPlayers[14]));
      stringlen:= ord(kopreceivedstring[16]);
      Delete(receivedstring, 1, Length(receivedstring[4])+18);
      hostname.Caption:=receivedstring;
        //gamemode
        Delete(kopreceivedstring,1,length(kopreceivedstring[4])+22+stringlen);
         label1.Caption:=kopreceivedstring;
         stringlenfromtext:=label1.GetTextLen;
        //Language
        Delete(kopreceivedstring2,1,length(kopreceivedstring2[4])+25+stringlenfromtext+stringlen);
        label2.Caption:=kopreceivedstring2;
      UDPClient.Active := False;
      UDPClient.Free;
  end;
end;

end.

Picture of result!

I Try to got players, which correct is 0 / 500 but i got 0 / 65533 I will be really enjoyed if you guys explain how it works.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
Andro
  • 1
  • Any chance you can translate your code to English too? Anyway, you will get a certain chunk of bytes from the server. TWO bytes make up the (max) number of players. You should not get these as the `ord` value of a character, but as a `word`. Since these are byte values, a string is not the best format to store it in in the first place. Better read it in stream or a byte array. You can use `ReceiveBuffer` for that, instead of `ReceiveString`. – GolezTrol Apr 27 '19 at 16:27
  • See [Proper use of TIdUDPClient.ReceiveBuffer](https://stackoverflow.com/questions/27137038/proper-use-of-idudpclient-receivebuffer) for an explanation on how to use it. – GolezTrol Apr 27 '19 at 16:35
  • I translated it :P – Andro Apr 30 '19 at 12:43
  • Thanks. I added the translation to the question. Did you have any luck with the `ReceiveBuffer` example I linked to? – GolezTrol Apr 30 '19 at 13:03
  • I was trying using this kind of ReceiveBuffer, all the time i got error that Message is to long,but when i try to receive these bytes it returns 0 – Andro May 01 '19 at 00:27

0 Answers0