选择File -> New -> Console Application - Delphi
,然后复制并粘贴以下代码。
注意:在 Delphi 2010 下为我工作。
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils // misc
,IdUDPClient // UDP Client
;
procedure DumpBytes(const Value: TBytes; const ACount: Integer);
const
CLINE_BREAK_AT = 10;
var
Index: Integer;
LHex: string;
LBreakCnt: Integer;
begin
Writeln('Response dump');
Writeln(StringOfChar('-', 50));
LHex := EmptyStr;
LBreakCnt := 0;
for Index := Low(Value) to ACount -1 do begin
LHex := LHex + '0x' + IntToHex(Value[Index], 2) + ' ';
Inc(LBreakCnt);
if LBreakCnt = CLINE_BREAK_AT then begin
Writeln(LHex);
LBreakCnt := 0;
LHex := EmptyStr;
end;
end;
Writeln(StringOfChar('-', 50));
end;
procedure DoTheDo;
var
LClient: TIdUDPClient;
LRequest: TBytes;
LResponse: TBytes;
LReadBytes: Integer;
begin
LClient := TIdUDPClient.Create(NIL);
try
LClient.Host := 'ru5-dayz.myfabis.ru';
LClient.Port := 2302;
LClient.Connect;
if LClient.Connected then begin
Writeln('Connection established to: ', LClient.Host, ' on port: ', LClient.Port);
LRequest := TBytes.Create($FE,$FD,$00,$21,$21,$21,$21,$FF,$00,$00,$00);
LClient.SendBuffer(LClient.Host, LClient.Port, LRequest);
SetLength(LResponse, 4096);
LReadBytes := LClient.ReceiveBuffer(LResponse, 1000);
Writeln('Received: ', LReadBytes, ' bytes');
DumpBytes(LResponse, LReadBytes);
end else
Writeln('woops! can''t connect!!');
finally
LClient.Free;
end; // tryf
end;
begin
try
WriteLn('Press [Enter] to begin...');
ReadLn;
///
/// main procedure
///
DoTheDo;
///
/// give the user time to see what happened
///
Readln;
except
on E: Exception do begin
Writeln('Ouch! ', E.ClassName, ': ', E.Message);
ReadLn;
end;
end;
end.