Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Apr : [D7] Broadcasting with UDP?

www.cryer.info
Managed Newsgroup Archive

[D7] Broadcasting with UDP?

Subject:[D7] Broadcasting with UDP?
Posted by:"John Doe" (john.d..@acme.com)
Date:Thu, 27 Apr 2006 06:54:29

Hello

    I'd like to broadcast a string to all hosts on a LAN using
Indy's TidUDPClient on a form. I know very little about Indy, so it
might something very simple but I'm stuck :-/

Currently, with both programs running on the same host, I have...

1. NetCat listening on a port using:

nc -vv -u -L -p 1234

(-vv = very verbose; -u = UDP; -L = listen but don't close -p = bind
to port)

2. and I run the following code from the same host

========= CODE =========
procedure TForm1.Button1Click(Sender: TObject);
const
  UDPDefaultPort = 1234;
var
  //LATER x: Array[0..20] of Byte;
  x: Array[0..4] of Byte;
  s : String;
  i : Integer;

begin
  IdUDPClient1.Port := UDPDefaultPort;

  //THIS WORKS
  //IdUDPClient1.Host := '127.0.0.1';
  //THIS DOESN'T
  IdUDPClient1.Host := '255.255.255.255';

  IdUDPClient1.Active := true;
  IdUDPClient1.BroadcastEnabled := True;

  //HELLO
  x[0] := $48;
  x[1] := $45;
  x[2] := $4C;
  x[3] := $4C;
  x[4] := $4F;

  {
  DOESN'T WORK

  s := 'THIS IS SOME STRING' + #13#10;
  for i := 0 to Length(s)-1 do begin
    //[Error] Incompatible types: 'Byte' and 'String'
    //x[i] := Copy(s,i,1);

    //FOUND THIS ON THE NET, BUT YUCK!    
    x[i] := ord(s[i + 1]) - 48;
  end;
  }

  //MUST USE EXCEPTION TO STOP INDY'S ANNOYING ERR 10004
  try
    IdUDPClient1.SendBuffer(x, 5);
  finally
  end;

  IdUDPClient1.BroadcastEnabled := False;

end;
========= CODE =========

=> NetCat does receive HELLO when I use 12.0.0.1 as host, but nothing
if I use 255.255.255.255. Any idea?

Thank you for any tip.

Replies:

www.cryer.info
Managed Newsgroup Archive