unit chat;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp;
type
TForm1 = class(TForm)
PortLabel: TLabel;
Port: TEdit;
Protokoll: TGroupBox;
Memo1: TMemo;
User: TLabel;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Server: TServerSocket;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ServerClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);
procedure ServerClientError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure ServerClientRead(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
Userzahl: Integer;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Server.Port:=strtoint (Port.Text);
Server.Active:=True;
Button1.Enabled:=False;
Button2.Enabled:=True;
Memo1.Lines.Add('Status: Server gestartet.') ;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Server.Port:=strtoint (Port.Text);
Server.Active:=False;
Button1.Enabled:=True;
Button2.Enabled:=False;
Memo1.Lines.Add('Status: Server angehalten') ;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
Memo1.Lines.Clear;
Memo1.Lines.Add('Serversoftware version1.0') ;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Server.Active:=False;
Application.Terminate;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Server.Active:= False;
end;
procedure TForm1.ServerClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('Status: Client ' + Socket.RemoteAddress +'verbunden.');
Userzahl:=Userzahl +1;
User.Caption:='User: ' + inttostr(Userzahl);
end;
procedure TForm1.ServerClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('Status: Client ' + Socket.RemoteAddress +'getrennt.');
Userzahl:=Userzahl -1;
User.Caption:='User: ' + inttostr(Userzahl);
end;
procedure TForm1.ServerClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
Memo1.Lines.Add('Status: Client ' + Socket.RemoteAddress + 'hat den Fehler:' + inttostr(Errorcode));
Userzahl:=Userzahl -1;
User.Caption:='User: ' + inttostr(Userzahl);
end;
procedure TForm1.ServerClientRead(Sender: TObject; Socket: TCustomWinSocket);
var i:integer;
Nachricht:AnsiString;
begin
Nachricht:=Socket.RemoteAddress + ': ' + Socket.ReceiveText;
Memo1.Lines.Add(Nachricht);
for i := 0 to Server.Socket.ActiveConnections - i do
Server.Socket.Connections[i] .SendText[Nachricht]; //Hier steht der Fehler laut Delphi
end;
end.