unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, ExtCtrls, ComCtrls, Sockets, WinSock;
type
TForm1 =
class(TForm)
Client: TClientSocket;
Memo1: TMemo;
Timer1: TTimer;
TCP: TTcpClient;
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure ClientError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure Timer1Timer(Sender: TObject);
procedure ClientRead(Sender: TObject; Socket: TCustomWinSocket);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
Nachricht_E,
IP :
String;
verg :
string;
implementation
{$R *.dfm}
procedure TForm1.ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
//Server benachrichtigen dass Client online ist
Socket.SendText('
online');
Memo1.Lines.Add('
Client online');
Timer1.Enabled := False;
Memo1.Lines.Add('
Timer ended');
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
//Server benachrichtigen dass Client offline ist
Client.Socket.SendText('
offline');
Memo1.Lines.Add('
Client offline');
Timer1.Enabled := False;
end;
procedure TForm1.ClientRead(Sender: TObject; Socket: TCustomWinSocket);
begin
//Empfangene Nachricht einer Variable zuweißen
Nachricht_E := Socket.ReceiveText;
//IP des Senders einer Variable zuweißen
IP := Socket.RemoteHost;
//Wenn empfangene Nachricht = close dann
if Nachricht_E = '
close'
then
begin
//Verbindung trennen
Memo1.Lines.Add('
Server closed');
Client.Active := False;
Timer1.Enabled := True;
end;
if Nachricht_E = '
accept'
then
begin
Timer1.Enabled := False;
Memo1.Lines.Add('
Timer ended');
Memo1.Lines.Add('
Client accepted');
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
if not (Client.active)
then
begin
Client.Active := True;
Memo1.Lines.Add('
Online sended');
end;
if (Client.active)
then
begin
Timer1.Enabled := False;
end;
end;
procedure TForm1.ClientError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
Client.Socket.Close;
Client.Active := False;
Timer1.Enabled := True;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Client.Active := True;
if not (Client.active)
then
begin
Timer1.Enabled := True;
Memo1.Lines.Add('
Timer started');
end;
if (Client.active)
then
begin
Timer1.Enabled := False;
end;
end;
end.