2013-04-14 128 views
-3

请帮助将此PHP更改为Delphi。这里的一个类似的问题帮助:Porting PHP code to Delphi code将PHP代码移植到Delphi代码

<?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

http://codereview.stackexchange.com –

+1

下,我想即使CR想在这里看到一些之前的努力。 OP,你可以先放一下吗? – halfer

+2

[将PHP代码移植到Delphi代码3]的可能副本(http://stackoverflow.com/questions/16000764/porting-php-code-to-delphi-code-3) – halfer

回答

3

选择File -> New -> Console Application - 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.