-3

Please help to change this PHP to Delphi. A similar question here helped: 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);
?>

Here's my attempt so far:

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 := '';
Community
  • 1
  • 1
  • http://codereview.stackexchange.com – Hanky Panky Apr 14 '13 at 15:56
  • 1
    I should think even CR would like to see some prior effort here. OP, can you give this a go first? – halfer Apr 14 '13 at 15:58
  • 2
    possible duplicate of [Porting PHP code to Delphi code 3](http://stackoverflow.com/questions/16000764/porting-php-code-to-delphi-code-3) – halfer Apr 14 '13 at 15:59
  • http://www.freelancer.com/ – Sam Dufel Apr 14 '13 at 16:00
  • 1
    Aha, and a direct dup of a question you asked half an hour ago, which was closed. Please don't do that - that's classic [help vampire](http://slash7.com/2006/12/22/vampires/) behaviour. – halfer Apr 14 '13 at 16:00
  • 1
    Do not ask the same question again and again. If your question gets closed, edit it and try to get it re-opened. – David Heffernan Apr 14 '13 at 16:03
  • Undownvoted, thanks for editing. I'll vote to reopen if this now closes. – halfer Apr 14 '13 at 16:04
  • That subject was closed. – user2279875 Apr 14 '13 at 16:04
  • 2
    Correct. Edit that question and we can reopen that, if you make it a good question. Don't spam the site with the same question over and over. Doing that will lead to a ban. – David Heffernan Apr 14 '13 at 16:05
  • Subject edited, what you want me to do. – user2279875 Apr 14 '13 at 16:08
  • 1
    look at TIdTCPClient, set host, port, then call TIdTCPClient.Connect, when connection is established you can then use IOHandler.Write([$FE,$FD,$00,$21,$21,$21,$21,$FF,$00,$00,$00]); have fun! –  Apr 14 '13 at 16:09
  • 1
    Personally, I think it is OK to persist with this question now that it has been edited to show a prior attempt. Ping me if it closes please. – halfer Apr 14 '13 at 16:15
  • 2
    It's still no good as a question. It fails to say how the function failed, what it's expected to do and so on. It's a "write my code for me" question. It should be downvoted, closed and deleted. – David Heffernan Apr 14 '13 at 16:26
  • 3
    @DavidHeffernan ahhh, he's new, give him a chance, he'll learn... –  Apr 14 '13 at 16:27
  • StackOverflow still is not a "Write my code for me" site and most certainly not a "Figure out by yourself what my code is supposed to do" site either. – Jerry Dodge Apr 14 '13 at 16:31
  • 2
    @JerryDodge you're not forced to share, you can simply ignore it, many times when I search for something web related, I see 3+ questions on SO being asked differently, but the answer is basically the same, those questions are **not** deleted –  Apr 14 '13 at 16:37
  • I'm sure those questions were also from at least 2-3 years ago when the rules here were much looser and there weren't as many questions as there are today. – Jerry Dodge Apr 14 '13 at 16:46
  • 2
    This is quite clearly "too localized". However, in practice we very often help people and answer such Qs. However, it's a poor question. 1. What is the code expected to do? 2. In what way does it fail to do that? Those basic questions are not addressed. – David Heffernan Apr 14 '13 at 16:48
  • 1
    @user2279875 **what** doesn't work? Which line of code do you have problems with? Any error messages? Any unexpected results? Are you confused about one particular line of code? These are things you always need to address to us so we can clearly understand your problem. – Jerry Dodge Apr 14 '13 at 17:18
  • 1
    We also do not know even which version of Delphi you are using, which could make a huge difference. – Jerry Dodge Apr 14 '13 at 17:22

1 Answers1

3

Select File -> New -> Console Application - Delphi, then copy and paste the below code.

NOTE: works for me under 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.
halfer
  • 19,824
  • 17
  • 99
  • 186