unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, IdBaseComponent, IdComponent, IdTCPServer, StdCtrls;
type
TForm1 =
class(TForm)
IdTCPServer1: TIdTCPServer;
Timer1: TTimer;
Label1: TLabel;
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure Timer1Timer(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
type
TClientClass =
class
private
PeerThread: TIdPeerThread;
end;
var
Form1: TForm1;
fClientList: TList;
//Liste für verbundene Client
ListData: TStringList;
//In dieser Liste landen die Daten, die dann an die Clienten verschickt werden
implementation
{$R *.dfm}
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
AThread.Data := TClientClass.Create();
fClientList.Add(AThread.Data);
TClientClass(AThread.Data).PeerThread := AThread;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
fClientList := TList.Create;
ListData := TStringList.Create;
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
fClientList.Free;
ListData.Free;
end;
procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
fClientList.Delete(fClientList.IndexOf(AThread.Data));
end;
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
i: integer;
LastIndex: integer;
NoData: integer;
lCount: integer;
begin
LastIndex := ListData.Count;
while AThread.Connection.Connected
do
begin
Sleep(1);
if ListData.Count > LastIndex
then
begin
NoData := 0;
lCount := ListData.Count;
for i := lCount - 1 - LastIndex
downto 0
do
AThread.Connection.WriteLn(ListData.Strings[i] + '
|~|');
LastIndex := lCount;
end
else // Wenn die Verbidunung zu einem Client unerwartet
begin // abgebrochen wird, merkt das der Server erst, wenn
NoData := NoData + 1;
// er versucht, irgendwas zu schicken. Dafür ist dieses
if NoData >= 100
then // Teil da.
begin
AThread.Connection.WriteLn();
NoData := 0;
end;
end;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Label1.Caption := IntToStr(fClientList.Count) + '
aktive Verbindungen';
end;
end.