请帮助将此PHP更改为Delphi。这里有一个类似的问题:Porting PHP code to Delphi code
<?php
$fp = @fsockopen("udp://ru5-dayz.myfabis.ru", 2302, $errno, $errstr, 1);
fwrite($fp, "\xFE\xFD\x00\x21\x21\x21\x21\xFF\x00\x00\x00");
$buffer = fread($fp, 4096);
$buffer = substr($buffer, 5, -2);
if (!$buffer) { return FALSE; };
$item = explode("\x00", $buffer);
var_dump($item);
?>
到目前为止,这是我的尝试:
UdpSocket1.BlockMode := bmNonBlocking;
UdpSocket1.RemoteHost := arr[combobox2.ItemIndex][1];
UdpSocket1.RemotePort := arr[combobox2.ItemIndex][2];
UdpSocket1.Open;
UdpSocket1.Sendln('þý!!!!ÿ', ' ');
UdpSocket1.ReceiveBuf(tempS, 4096);
UdpSocket1.WaitForData(1000);
Memo1.Text := '';
答案 0 :(得分:3)
选择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.