unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdUDPServer, IdStackConsts, IdSocketHandle, StdCtrls;
type
TUDPServer =
Class(TIdUDPServer)
Protected
function GetBinding: TIdSocketHandle;
override;
end;
TForm1 =
class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
_UDPServer : TUDPServer;
{ Private declarations }
public
{ Public declarations }
procedure UDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TUDPServer.GetBinding: TIdSocketHandle;
var
i: integer;
begin
if not Assigned(FCurrentBinding)
then
begin
if Bindings.Count < 1
then begin
Bindings.Add;
end;
for i := 0
to Bindings.Count - 1
do begin
Bindings[i].AllocateSocket(Id_SOCK_DGRAM);
Bindings[i].SetSockOpt(Id_SOL_SOCKET,Id_SO_REUSEADDR,@Id_SO_True,SizeOf(Bool));
// Bis auf diese Zeile ist alles kopiert von TIdUDPServer
Bindings[i].Bind;
end;
FCurrentBinding := Bindings[0];
FListenerThread := TIdUDPListenerThread.Create(BufferSize, Self);
FListenerThread.Start;
BroadcastEnabledChanged;
end;
Result := FCurrentBinding;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
// Dies ist nur notwendig, da ich die Indy-Palette nicht installiert habe.... ansonsten per OI
_UDPServer:=TUDPServer.Create(Self);
_UDPServer.DefaultPort:=4444;
_UDPServer.Active:=True;
_UDPServer.OnUDPRead:=UDPRead;
end;
procedure TForm1.UDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
Var
S :
String;
Begin
SetLength(S,4);
AData.
Read(S[1],4);
Caption:=S;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
_UDPServer.Broadcast('
test',4444);
end;
end.